我正在用JavaScript制作一个Rock,Paper,Scissors游戏。我的老师要我用for循环来重复转弯。除此之外,它需要在for循环中进行10场比赛,我需要在每轮比赛后打印出比分,然后一旦比赛结束,我必须打印总分。
请帮助我不知道该怎么做。我一直在尝试过去4个小时,但我找不到任何特定问题的答案。
for(i = 0; i < 3; i++) {
// User choice
var userChoice = prompt("Do you choose rock, paper or scissors?");
if (! userChoice) {
// User choice was undefined
document.write("<p>Player 1, you cheated! Refresh this screen and fight like a man.</p>");
} else {
// Display user choice
document.write("<p>Player 1:" + " " + userChoice + "</p>");
}
// Computer choice
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
// Display computer choice
document.write("<p>Computer:" + " " + computerChoice + "</p>");
// Compare user choice vs computer choice
var compare = function(choice1,choice2) {
if (choice1 === choice2) {
return "It's a tie!";
}
if (choice1 === "rock") {
if (choice2 === "scissors") {
// rock wins
return "You win!";
} else {
// paper wins
return "You lose! Try again.";
}
}
if (choice1 === "paper") {
if (choice2 === "rock") {
// paper wins
return "You win!";
} else {
// scissors wins
return "You lose! Try again.";
}
}
if (choice1 === "scissors") {
if (choice2 === "rock") {
// rock wins
return "You lose! Try again.";
} else {
// scissors wins
return "You win!";
}
}
};
// Run the compare function
var results = compare(userChoice,computerChoice);
// Display results
document.write("<br><hr><b>Results: </b>" + results);
}
答案 0 :(得分:0)
您在评论的粘贴框中发出的是您在尝试返回值后添加得分。返回后的代码无法访问。您需要先添加该值。此外,你可以赢得+ = 1作为一个小速记。 :)
var win = 0;
var loss = 0;
var tie = 0;
for(i = 0; i < 3; i++) {
// User choice
var userChoice = prompt("Do you choose rock, paper or scissors?");
if (! userChoice) {
// User choice was undefined
document.write("<p>Player 1, you cheated! Refresh this screen and fight like a man.</p>");
} else {
// Display user choice
document.write("<p>Player 1:" + " " + userChoice + "</p>");
}
// Computer choice
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
// Display computer choice
document.write("<p>Computer:" + " " + computerChoice + "</p>");
// Compare user choice vs computer choice
var compare = function(choice1,choice2) {
if (choice1 === choice2) {
tie+=1;
return "It's a tie!";
}
if (choice1 === "rock") {
if (choice2 === "scissors") {
// rock wins
win+=1;
return "You win!";
} else {
// paper wins
loss+=1;
return "You lose! Try again.";
}
}
if (choice1 === "paper") {
if (choice2 === "rock") {
// paper wins
win+=1;
return "You win!";
} else {
// scissors wins
loss+=1;
return "You lose! Try again.";
}
}
if (choice1 === "scissors") {
if (choice2 === "rock") {
// rock wins
loss+=1;
return "You lose! Try again.";
} else {
// scissors wins
win+=1;
return "You win!";
}
}
};
// Run the compare function
var results = compare(userChoice,computerChoice);
// Display results
document.write("<br><hr><b>Results: </b>" + results);
}
document.write("<br/>Final Results: W-"+win+"; L-"+loss+"; T-"+tie+";");