简体   繁体   English

While循环用于JavaScript中的错误检查

[英]While loops for error checking in JavaScript

I am trying to get this to prompt a user to pick a color, either red, green or blue. 我试图让它提示用户选择一种颜色,红色,绿色或蓝色。 and based off of their answer the program will display a message. 根据他们的回答,程序将显示一条消息。 I am trying to use the while loop to check to see if the user picked either red, green or blue, if they did not it will prompt again to chose only from red, green or blue. 我正在尝试使用while循环来检查用户是否选择了红色,绿色或蓝色,如果没有选择,它将再次提示您仅选择红色,绿色或蓝色。

My problem is that the program seems to be getting stuck in the while loop, no matter what they answer. 我的问题是,无论他们回答什么,该程序似乎都陷入了while循环中。 I don't know what I did wrong. 我不知道我做错了什么。

I know there is probably another way to do this but this is a school assignment and I have to use a while loop for this. 我知道可能还有另一种方法,但这是学校的作业,为此我必须使用while循环。

var color = prompt("What is your favorite color: red, green, or blue?");
    color =color.toLowerCase();
    color = color.trim();


while(color != 'red' || color != 'blue' || color != 'green'){

     var color = prompt("Please choose from either: red, green, or blue?");
}

if (color == 'red'){

document.write('Spicy!');

}else if(color == 'green'){

    document.write('Green is such a mellow color.');

}else{

document.write('Wow! Blue is my favorite color too!');

}

Let's step through your while exit condition: 让我们逐步了解您的while退出条件:

  • colour != 'red Okay, so if it's not red, keep looping. colour != 'red好的,如果不是红色,请继续循环播放。 What if it is red? 如果红色怎么办?
  • || colour != 'blue' || colour != 'blue' Huh. || colour != 'blue'呵呵。 But the only time we get here is if it's red. 但是,我们唯一到达这里的时间是红色。 If it's red, it's certainly not blue, so the loop never ends. 如果是红色,则肯定不是蓝色,因此循环永远不会结束。

Try && instead of || 尝试&&代替|| .

You define two vars called color: 您定义了两个称为color的变量:

change 更改

var color = prompt("Please choose from either: red, green, or blue?");

to

color = prompt("Please choose from either: red, green, or blue?");

so it uses the colour var 'outside' the while. 因此它在while之外使用了颜色var。 Also the ||'s should be &&'s. 而且||应该是&&。

Just to be clear: 只是要清楚:

var color;

do
{
    color=prompt("What is your favorite color: red, green, or blue?");
    color=color.toLowerCase().trim();
}
while(color!='red' && color!='blue' && color!='green');

switch(color)
{
   case 'red':
       document.write('Spicy!');
       break;

   case 'green':
       document.write('Green is such a mellow color.');
       break;

   case 'blue':
       document.write('Wow! Blue is my favorite color too!');
       break;
}

you define var color twice, is there any reason for that? 您两次定义var color ,有什么原因吗? either way this while will do the job:: 无论哪种方式,这将做的工作::

    while (color) {
        if (color == "red") {
            console.log("red");
            break;
        }else if (color=="blue") {
            console.log("blue");
            break;
        }
        break;
    }

if you have color != 'red' and if this color never is pass the loop will be infinity, so in this way we break the while either way since you only expect some predefined values 如果您有color != 'red'并且如果这种颜色从不通过,则循环将为无穷大,因此,由于您只期望一些预定义的值,因此我们以两种方式中断了while循环

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

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