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

发布于 2025-01-11 20:09:30 字数 1282 浏览 0 评论 0原文

我一直在思考如何让这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明明#如月 2025-01-18 20:09:30

您可以将此行更改

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

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

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

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.

五里雾 2025-01-18 20:09:30

您需要更新循环中的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 循环 - 如果输入的答案不可接受,则会显示错误警报 &游戏重新开始。

You need to update the input variable in the loop.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文