简体   繁体   English

即使条件已满足,while循环也不会停止

[英]while loop doesnt stop even after condition has been fulfilled

Functionality: 功能:

User is to input the color based on a guessing game. 用户将基于猜谜游戏输入颜色。 Prompt will keep prompt when the answer is wrong, prompt will only exit when the user set the correct answer. 当答案错误时,提示将保持提示,仅当用户设置正确答案时,提示才会退出。

WHat I have done: Have made use of a while loop to check the condition. 我做了什么:利用while循环检查条件。 In while loop, have set the while loop to finish only when it is equivalent to target, and target is a randomly picked color by the system 在while循环中,将while循环设置为仅在与target等效时才结束,并且target是系统随机选择的颜色

Issue: Prompt initially set the color as the answer but prompt kept looping even after user has set the correct answer. 问题:提示最初将颜色设置为答案,但是即使用户设置了正确的答案,提示仍然不断循环播放。

How to exit loop when one of the color is set? 设置一种颜色时如何退出循环?

 <html> <head> <title>Color Guessing Game</title> </head> <body onload="do_game()"> <script> function do_game() { var color = ["blue", "cyan", "gray", "green", "magenta", "orange", "red", "white", "yellow"]; var guess_input_text, guess_input, finished = false, target, guesses = 0; var target_index = color[0, 8]; target = target_index; alert(target); while (!finished) { guess_input_text = prompt("I am thinking of these colors:" + "\\n\\n blue, cyan, gray, green, magenta, orange, red, white, yellow" + "\\n\\n what color am I thinking of?"); guess_input = parseInt(guess_input_text); guesses += 1; finished = target; } } </script> </body> </html> 

Try this code which controls every round of the loop if the input is the same as the target and if they're the same then finished gets true: 尝试以下代码,如果输入与目标相同,并且输入相同,则控制循环的每一步,然后完成返回true:

function do_game() {
    var color = ["blue", "cyan", "gray", "green", "magenta", "orange", "red", "white", "yellow"];
    var guess_input_text, guess_input, finished = false,
      target, guesses = 0;

    var rnd = Math.floor((Math.random() * 9) + 0); //Makes a random number between 0 and 8
    target = color[rnd]; //sets the target to a random color from the color array
    while (!finished) {
      guess_input_text = prompt("I am thinking of these colors:" +
        "\n\n blue, cyan, gray, green, magenta, orange, red, white, yellow" + "\n\n what color am I thinking of?"); //gets alert-input from user
      guesses += 1;
      if(guess_input_text == target){//checks if input from user and target are the same
        finished = true;
      }
    }
  }

Another way of doing it without finished variable. 没有完成变量的另一种方法。 Using break 使用休息

var target_text = color[0, 8];
...
while (true) {
      guess_input_text = prompt("I am thinking of these colors:" +
        "\n\n blue, cyan, gray, green, magenta, orange, red, white, yellow" + "\n\n what color am I thinking of?");

      guesses += 1;
      if(guess_input_text == target_text){
       //guessed correct answer
       break;
      }
}

Your code doesn't do what you think it does. 您的代码没有执行您认为的操作。

This doesn't get the index of a random value, it gets the last value. 这不会获取随机值的索引,而是会获取最后一个值。

var target_index = color[0,8];

This will get a random array value: var rand = color[Math.floor(Math.random() * color.length)]; 这将获得一个随机数组值:var rand = color [Math.floor(Math.random()* color.length)];

Once you have the winning value, there's no point in doing an index comparison. 一旦获得了获胜的价值,就没有必要进行索引比较了。 You can simply compare strings, as prompt() returns the string of the input response (modified prompt text for brevity). 您可以简单地比较字符串,因为hint()返回输入响应的字符串(为简便起见,修改了提示文本)。

var finished = false;
while (!finished)
{
    var input = prompt("What color am I thinking of?");
    finished = (input.toLowerCase() === rand);
}

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

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