[英]JavaScript While-Loop not Working
I was trying to make a text adventure game when I encountered many problems. 当我遇到许多问题时,我正试图制作一款文字冒险游戏。 This was expected since I am fairly new to JavaScript, as well as coding in general.
这是意料之中的,因为我对JavaScript以及一般的编码还很陌生。 One of the problems I encountered was one where I added some code to prevent the possibility of someone not choosing something.
我遇到的问题之一是在其中添加一些代码以防止某人不选择某些东西的可能性。 No matter what input I gave the prompt window, it just became unresponsive.
无论我在提示窗口中输入了什么内容,它都没有响应。
Here's the code that I think is responsible: 这是我认为负责的代码:
var weapon = prompt("(prompt text)");
while (weapon != "axe" || weapon != "bow and arrow" || weapon != "rubber chicken"); {
alert("That's not an option!");
weapon = prompt("(prompt text)");
}
while (weapon != "axe" ... weapon != "rubber chicken");
The main problem is the ;
主要问题是
;
at the end. 在末尾。 It says the
while
loop has only one statement and ;
它说
while
循环只有一个语句;
is that statement. 是那句话。 It is similar to doing
类似于做
while (weapon != "axe" ... weapon != "rubber chicken") {
;
}
Just remove that ;
删除它
;
, you should be fine. ,您应该可以。
Note: Since you want to allow only those three values, the condition should have been with the &&
operator 注意:由于您只想允许这三个值,因此该条件应该已经在
&&
运算符中
while (weapon != "axe" && ... && weapon != "rubber chicken") {
...
}
When you use ||
使用
||
operator, if I input weapon
as rubber chicken
, since it doesn't match axe
the condition will be satisfied and you will be asking the same question again and again. 运算符,如果我输入的
weapon
是rubber chicken
,因为它与axe
不匹配,则条件将得到满足,您将一次又一次地问相同的问题。 When you use &&
operator, it will be truthy only when the value is not equal to all the three values you are comparing. 当使用
&&
运算符时,仅当该值不等于要比较的所有三个值时,它才是真实的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.