【发布时间】:2015-12-05 07:52:40
【问题描述】:
我刚刚在 codecademy 上完成了一个 rps 游戏,还有一些额外的任务,包括在用户输入错误信息时生成结果。基本上,如果您不输入“rock”、“paper”或“scissors”,它会再次询问您,直到您输入正确的。
我怎样才能调用 userChoice 变量,直到它得到正确的答案。我像这样尝试过,但它只询问 2 次,然后发布你写的任何内容。
var userChoice = prompt("Do you choose rock, paper or scissors?");
if (userChoice != "rock", "paper", "scissors") {
userChoice = prompt("Do you choose rock, paper or scissors?");
}
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
}
else if(choice1 === "rock") {
if ( choice2 === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
}
else if(choice1 === "paper") {
if(choice2 === "rock") {
return "paper wins";
}
else {
return "scissors wins";
}
}
else if(choice1 === "scissors") {
if(choice2 === "rock") {
return "rock wins";
}
else {
return "scissors wins";
}
}
}
console.log("Human: " + userChoice);
console.log("Computer: " + computerChoice);
compare(userChoice, computerChoice);
【问题讨论】:
-
你可以大大简化这个——我玩得很开心。看看这个,我加了cmets来解释快捷键:jsfiddle.net/svArtist/12nLz7nb
-
我明天去看看,谢谢建议
标签: javascript