我有一个摇滚,纸张,剪刀游戏设置,用户点击标有" Rock"," Paper"," Scissors"这导致userChoice。 当我运行程序时,compareChoices()函数总是返回"结果是平局,让我们再次玩!",我不明白为什么。
< article >
< button onclick = "rockPaperScissors('Rock')" > Rock < /button>
<button onclick="rockPaperScissors('Paper')">Paper</button >
< button onclick = "rockPaperScissors('Scissors')" > Scissors < /button>
</article >
< script type = "text/javascript" >
function rockPaperScissors(userchoice) {
alert("You chose " + userchoice + " ...the computer chose " + getComputerChoice() + ".");
compareChoices();
}
function getComputerChoice() {
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "Rock";
} else if (computerChoice < 0.67) {
computerChoice = "Paper";
} else {
computerChoice = "Scissors";
}
return computerChoice;
}
function compareChoices(userChoice, ComputerChoice) {
if (userChoice === ComputerChoice) {
alert("The result is a tie, let's play again!");
} else if (userChoice === "Rock") {
if (ComputerChoice === "Scissors") {
alert("Congratulations, you win!");
} else {
alert("The computer wins! Care to play again?");
}
} else if (userChoice === "Scissors") {
if (ComputerChoice === "Rock") {
alert("The computer wins, let's play again!");
} else {
alert("Yippie! You win!");
}
} else if (userChoice === "Paper") {
if (ComputerChoice === "Rock") {
alert("The computer wins. Don't give up, try again!");
} else {
alert("Hail the all mighty visitor. Give it another go!");
}
}
} < /script>
&#13;
答案 0 :(得分:2)
您没有将任何内容传递给compareChoices
< article >
< button onclick = "rockPaperScissors('Rock')" > Rock < /button>
<button onclick="rockPaperScissors('Paper')">Paper</button >
< button onclick = "rockPaperScissors('Scissors')" > Scissors < /button>
</article >
< script type = "text/javascript" >
function rockPaperScissors(userchoice) {
var computerChoice = getComputerChoice();
alert("You chose " + userchoice + " ...the computer chose " + computerChoice + ".");
compareChoices(userChoice, computerChoice);
}
function getComputerChoice() {
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "Rock";
} else if (computerChoice < 0.67) {
computerChoice = "Paper";
} else {
computerChoice = "Scissors";
}
return computerChoice;
}
function compareChoices(userChoice, ComputerChoice) {
if (userChoice === ComputerChoice) {
alert("The result is a tie, let's play again!");
} else if (userChoice === "Rock") {
if (ComputerChoice === "Scissors") {
alert("Congratulations, you win!");
} else {
alert("The computer wins! Care to play again?");
}
} else if (userChoice === "Scissors") {
if (ComputerChoice === "Rock") {
alert("The computer wins, let's play again!");
} else {
alert("Yippie! You win!");
}
} else if (userChoice === "Paper") {
if (ComputerChoice === "Rock") {
alert("The computer wins. Don't give up, try again!");
} else {
alert("Hail the all mighty visitor. Give it another go!");
}
}
} < /script>
&#13;
答案 1 :(得分:1)
您需要将参数传递给compareChoices()。
function rockPaperScissors(userchoice) {
alert("You chose " + userchoice + " ...the computer chose " + getComputerChoice() + ".");
compareChoices(userchoice, getComputerChoice());
}
你应该只提醒一次,因为他们会如此接近。像这样:
function compareChoices(userChoice, ComputerChoice) {
var message;
if (userChoice === ComputerChoice) {
message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - The result is a tie, let's play again!";
} else if (userChoice === "Rock") {
if (ComputerChoice === "Scissors") {
message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - Congratulations, you win!";
} else {
message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - The computer wins! Care to play again?");
}
} else if (userChoice === "Scissors") {
if (ComputerChoice === "Rock") {
message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - The computer wins, let's play again!";
} else {
message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - Yippie! You win!";
}
} else if (userChoice === "Paper") {
if (ComputerChoice === "Rock") {
message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - The computer wins. Don't give up, try again!";
} else {
message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - Hail the all mighty visitor. Give it another go!";
}
}
alert(message);
}
答案 2 :(得分:1)
你必须将计算机选择放入变量,并使用相同的计算机选择调用comparechoices函数。
function rockPaperScissors(userchoice) {
var computerchoice = getComputerChoice();
alert("You chose " + userchoice + " ...the computer chose " + computerchoice + ".");
compareChoices(userchoice, computerchoice);
}