我正在做一个石头剪刀布游戏,希望将验证添加到提示方法中,以便如果用户键入的单词不是“石头”,“纸张”或“剪刀”,那么我们将再次提示。
我似乎无法理解为什么下面的代码不起作用,即使我键入“ rock”,它也会提示我再次输入。
for (i = 0; i < 5; i++) {
let valid = false;
let askPlayer = prompt("Rock, Paper or Scissor?");
while(!valid) {
if (askPlayer !== "rock" || askPlayer !== "paper" || askPlayer !== "scissor" || askPlayer !== "Rock" || askPlayer !== "Paper" || askPlayer !== "Scissor") {
askPlayer = prompt("Enter again. You seem to have entered an invalid value");
}
else { valid = true; }
}
play(askPlayer, computerPlay());
}
最佳答案
我认为您可能会混淆肯定派和否定派。您的条件是:“如果玩家提供的值与上述任何一项都不匹配,则再次询问”
更人性化的条件是:
var valid_responses = ["rock","paper","scissors"];
if (valid_responses.includes(askPlayer.toLowerCase())){
valid = true;
} else {
askPlayer = prompt("Enter again. You seem to have entered an invalid value");
}