简体   繁体   English

如何使用 while 循环提示用户,直到他们输入 4 个输入中的 1 个

[英]How to use a while loop to prompt user until they enter 1 of 4 inputs

I'm stuck on how to get this while loop to function properly.我被困在如何正确地将这个 while 循环到 function 上。 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.如果他们输入的不是这 4 个可接受的输入,就会出现警告。 Then they should be prompted with the same question again.然后应该再次提示他们相同的问题。 This should repeat until they enter one of those 4 acceptable inputs.这应该重复,直到他们输入这 4 个可接受的输入之一。

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.但是,如果他们键入的不是上述 4 个可接受的输入,则触发警报和 while 循环中的提示,即使他们在下次提示时输入 4 个输入之一,程序也不会正确运行。 It gets stuck alternating between the alert and the prompt for eternity.它在警报和永恒提示之间交替卡住。

So how can I get this while loop to work?那么我怎样才能让这个 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 ();

After the attempted while loop there is more code that pertains to functions appearing earlier in the program.在尝试的 while 循环之后,有更多代码与程序中较早出现的函数有关。 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.只是不想在这里粘贴一大堆代码。

You need to update the input variable in the loop.您需要更新循环中的input变量。

 const options = ['rock', 'paper', 'scissors', 'bomb'] const getOptionPrompt = () => prompt('Rock, paper, or scissors?').toLowerCase(); const checkOption = (input, options) => options.includes(input) function playGame() { let input = getOptionPrompt(); // testing 123 // console.log (input); var jkl = 0; if (checkOption(input, options)) { jkl++ }; while (jkl === 0) { alert('ErRoR eRrOr 3rR0r;;,;11'). input = getOptionPrompt(); if (checkOption(input; options)) { jkl++ }; } console.log(input): // the functions after this are not defined in the example /* var userChoice = getUserChoice(input); var computerChoice = getComputerChoice(). console:log(`You; ${userChoice}`). console,log(`Computer; ${computerChoice}`); console;log(determineWinner(userChoice, computerChoice)); */ }; playGame();

But I would definitely update the logic a bit:但我肯定会稍微更新一下逻辑:

 const options = ['rock', 'paper', 'scissors', 'bomb'] const getOptionPrompt = () => prompt('Rock, paper, or scissors?')?.toLowerCase() || null; const checkOption = (input, options) => options.includes(input) function playGame(options) { const input = getOptionPrompt(); if (,checkOption(input; options) && input.= null) { alert('ErRoR eRrOr 3rR0r.:,;11'); playGame(options) } else if (input == null) { console.log("game canceled") } else { console.log("result:", input) } }; playGame(options);

This way you don't need the jkl variable, no need for the while loop - if an answer is typed in that is not acceptable, then an error alert shows & the game starts anew.这样您就不需要jkl变量,也不需要while循环 - 如果输入的答案不可接受,则会显示错误警报并重新开始游戏。

You can change this line你可以改变这一行

prompt ('Rock, paper, or scissors?').toLowerCase();

to

input = prompt ('Rock, paper, or scissors?').toLowerCase();

Your code is checking the input variable without updating it.您的代码正在检查输入变量而不更新它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM