Javascript:while循环永远循环 [英] Javascript: While loop looping forever

查看:90
本文介绍了Javascript:while循环永远循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个while循环循环在键入no的是时永远循环。请帮忙!

This while loop loop loops forever on typing "yes" of "no". Please help!

<script type="text/javascript">

var y = prompt("Do you want to enter values, type 'Yes' or 'No'!");

while ((y.toUpperCase()!="YES") || (y.toUpperCase()!="NO"))
{
	if (y.toUpperCase()=="YES")
	{
 	alert(y);
	}
	else if (y.toUpperCase()=="NO")
	{
 	alert(y);
	}
	else
	{
	alert("You have type \"" +y+ "\" which is no option! Please type again");
        y = prompt("Do you want to enter values, type 'Yes' or 'No'!");
	}

}
</script>

推荐答案

试试这个

Try this
var y = prompt("Do you want to enter values, type 'Yes' or 'No'!");

while (y.toUpperCase()!="NO")
{
    if (y.toUpperCase()=="YES")
    {
    	alert(y);
        break;
    }
    else
    {
        alert("You have typed \"" +y+ "\" which is not an option! Please type again");
        y = prompt("Do you want to enter values, type 'Yes' or 'No'!");
    }
}


这是因为你在while条件下检查了YES和NO(使用OR运算符)并允许所有内容并且再次提示。



您应该将OR更改为AND并尝试。
That is because you have checked both YES and NO (using OR operator) in your while condition and allowed everything and again prompting.

You should probably change OR to AND and try.


感谢您的回复。我完全理解你的答案。让迭代:

我输入是。

然后while循环开始

它检查(y!= 是)结果== false



然后检查(y!=no)结果== true



然后它继续在while循环中迭代。



谢谢你们!
Thanks for the response . I fully understand your answers.Let's iterate:
I type "yes".
Then the while loop starts
it checks (y != "yes") result == false

then it checks (y !="no) result == true

Then it goes on iterating in the while loop.

Thanks guys!!


这篇关于Javascript:while循环永远循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆