如何使用 while 循环提示用户,直到他们输入 4 个输入中的 1 个
我一直在思考如何让这个 while 循环正常运行。我希望程序提示用户一个问题“石头、布、剪刀?”他们可以正确输入“石头”、“布”、“剪刀”或“炸弹”(自动获胜的作弊代码......哈哈)。如果他们输入了这 4 个可接受的输入之外的任何内容,就会出现警报。然后他们应该再次被提示同样的问题。这应该重复进行,直到他们输入这 4 个可接受的输入之一。
到目前为止,如果他们在第一次出现提示时正确输入他们的选择,它就会起作用。但是,如果他们键入上述 4 个可接受的输入之外的内容,从而在 while 循环中触发警报和提示,则即使他们在下次提示时输入 4 个输入之一,程序也无法正确运行。它会在警报和提示之间交替卡住,直到永远。
那么我怎样才能让这个 while 循环工作呢?
function playGame () {
var input = prompt ('Rock, paper, or scissors?').toLowerCase();
// testing 123
// console.log (input);
var jkl = 0;
if (input === 'rock' || input === 'paper' || input === 'scissors' || input === 'bomb') {
jkl++
};
while (jkl === 0) {
alert ('ErRoR eRrOr 3rR0r!!!!11');
prompt ('Rock, paper, or scissors?').toLowerCase();
if (input === 'rock' || input === 'paper' || input === 'scissors' || input === 'bomb') {
jkl++
};
}
console.log (input);
var userChoice = getUserChoice (input);
var computerChoice = getComputerChoice ();
console.log (`You: ${userChoice}`);
console.log (`Computer: ${computerChoice}`);
console.log (determineWinner (userChoice, computerChoice));
};
playGame ();
在尝试 while 循环之后,有更多与程序中较早出现的函数相关的代码。忽略我的猜测。除非有人认为它相关,在这种情况下我也可以发布它。只是不想在这里粘贴大量的代码。
I'm stuck on how to get this while loop to function properly. I want the program to prompt the user with a question 'Rock, paper, or scissors?' to which they can correctly enter 'rock', 'paper', 'scissors', or 'bomb' (cheat code for auto-win... lol). If they type in anything other than those 4 acceptable inputs, an alert should appear. Then they should be prompted with the same question again. This should repeat until they enter one of those 4 acceptable inputs.
So far, it works if they type in their choice correctly the first time the prompt appears. However, if they type something other than the above 4 acceptable inputs, triggering the alert and the prompt in the while loop, the program does not run correctly even if they enter one of the 4 inputs the next time they are prompted. It gets stuck alternating between the alert and the prompt for eternity.
So how can I get this while loop to work?
function playGame () {
var input = prompt ('Rock, paper, or scissors?').toLowerCase();
// testing 123
// console.log (input);
var jkl = 0;
if (input === 'rock' || input === 'paper' || input === 'scissors' || input === 'bomb') {
jkl++
};
while (jkl === 0) {
alert ('ErRoR eRrOr 3rR0r!!!!11');
prompt ('Rock, paper, or scissors?').toLowerCase();
if (input === 'rock' || input === 'paper' || input === 'scissors' || input === 'bomb') {
jkl++
};
}
console.log (input);
var userChoice = getUserChoice (input);
var computerChoice = getComputerChoice ();
console.log (`You: ${userChoice}`);
console.log (`Computer: ${computerChoice}`);
console.log (determineWinner (userChoice, computerChoice));
};
playGame ();
After the attempted while loop there is more code that pertains to functions appearing earlier in the program. Ignore that I suppose. Unless anyone thinks it's relevant, in which case I can post that too. Just didn't want to paste a massive wall of code in here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将此行更改
为
您的代码正在检查输入变量而不更新它。
You can change this line
to
Your code is checking the input variable without updating it.
您需要更新循环中的
input
变量。但我肯定会稍微更新一下逻辑:
这样您就不需要
jkl
变量,也不需要while
循环 - 如果输入的答案不可接受,则会显示错误警报 &游戏重新开始。You need to update the
input
variable in the loop.But I would definitely update the logic a bit:
This way you don't need the
jkl
variable, no need for thewhile
loop - if an answer is typed in that is not acceptable, then an error alert shows & the game starts anew.