Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中的无限循环代码_Javascript_Infinite Loop - Fatal编程技术网

javascript中的无限循环代码

javascript中的无限循环代码,javascript,infinite-loop,Javascript,Infinite Loop,我的代码进入无限循环,即使我输入“是”或“是” 下面是我的代码: var answer = prompt("Are we there yet?"); while(answer != "yes" || answer != "yeah" ){ var answer = prompt("Are we there yet?"); } alert("YAY! We made it!!"); 将|更改为&& 你在问:如果你的答案不是“是”或者不是“是”。如果你回答“是”,答

我的代码进入无限循环,即使我输入“是”或“是”

下面是我的代码:

var answer = prompt("Are we there yet?");   
while(answer != "yes" || answer != "yeah" ){
    var answer = prompt("Are we there yet?");    
}    
alert("YAY! We made it!!"); 

|
更改为
&&

你在问:如果你的答案不是“是”或者不是“是”。如果你回答“是”,答案不是“是”。如果你回答“是”,你的答案不是“是”。这就是为什么你有一个无限循环

查看更多详细信息。谈判
!(答案==“yes”|答案==“yes”)
也会导致更改or运算符

var-answer=prompt(“我们到了吗?”);
while(回答!=“是”&&answer!=“是”){
回答=提示(“我们到了吗?”);
}

警惕(“耶!我们成功了!!”首先,不要重新定义
answer
(从循环中删除
var
)。其次,考虑您的循环有三种可能的情况:

answer = "yes"  ->  (answer != "yes") is false, (answer != "yeah") is true. The loop continues.
answer = "yes"  ->  (answer != "yes") is true, (answer != "yeah") is false. The loop continues.
answer = "anything else"  ->  (answer != "yes") is true, (answer != "yeah") is true. The loop continues.
使用逻辑AND
&&
运算符可以解决您的问题:

var-answer=prompt(“我们到了吗?”);
while(回答!=“是”&&answer!=“是”){
回答=提示(“我们到了吗?”);
}

警惕(“耶!我们成功了!!”你的问题是什么?使用
&&
而不是
|
。答案总是不是“是”就是不是“是”。你想要的是
&
,而不是
|
。当
答案==“yes”
时,它就不等于
“yes”
。当
answer==“yes”
时,假设您键入
“yes”
,则它不等于
“yes”
。那么您的条件是“如果为假或为真,请重试”。如果您键入
yeah
,则为“如果为真或假,请重试”。如果您键入其他内容,则为“如果为真或为真,请重试”。没有代码不重试的情况。不需要定义两次答案。@SagiRika首先,如果您检查时间,我会在答案出现在注释中之前开始回答。第二项检查:“答案应该是答案”。但这是很明显的。。。您不能将注释标记为正确答案。我的意思是您使用了两次
var-answer
,但我可以看到您现在编辑了它。@SagiRika噢,该死的。对此我非常抱歉。我觉得有点生气。我只是复制了问题代码。再次抱歉。