繁体   English   中英

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

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

我被困在如何正确地将这个 while 循环到 function 上。 我希望程序提示用户一个问题“石头、布还是剪刀?” 他们可以在其中正确输入“石头”、“布”、“剪刀”或“炸弹”(自动获胜的作弊码……哈哈)。 如果他们输入的不是这 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 循环之后,有更多代码与程序中较早出现的函数有关。 忽略我想的。 除非有人认为它是相关的,否则我也可以发布它。 只是不想在这里粘贴一大堆代码。

您需要更新循环中的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();

但我肯定会稍微更新一下逻辑:

 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);

这样您就不需要jkl变量,也不需要while循环 - 如果输入的答案不可接受,则会显示错误警报并重新开始游戏。

你可以改变这一行

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

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

您的代码正在检查输入变量而不更新它。

暂无
暂无

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

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