简体   繁体   English

我无法让do while循环在javascript中工作

[英]I can not get my do while loop to work in javascript

Here is my code. 这是我的代码。 I am trying to get it to ask 1. "What would you like to eat" and then 2. ask again if userEntree is not burger, chicken, or fish. 我正在尝试让它询问1.“您想吃什么”,然后2.再询问userEntree是否不是汉堡,鸡肉或鱼。

var userEntree = prompt('What entree would you like?')

do {
   var userEntree = prompt('What entree would you like?')
}
while(userEntree.toLowerCase() != "burger" || 
      userEntree.toLowerCase() != "chicken" || userEntree.toLowerCase() != 
      "fish") 

You're using the wrong operator: 您使用了错误的运算符:

(userEntree.toLowerCase() != "burger" && userEntree.toLowerCase() != "chicken" && userEntree.toLowerCase() != "fish")

"Do something until the user's answer is chicken or burger or fish" translates into a while-condition as "Do something while the answer is not chicken and not burger and not fish." “做某事, 直到用户的答案是鸡肉汉堡鱼”,翻译成一会儿条件,即“做某事 ,答案不是鸡肉汉堡而不是鱼”。

Also, the first var userEntree = prompt('What entree would you like?') is unnecessary; 同样, var userEntree = prompt('What entree would you like?')第一个var userEntree = prompt('What entree would you like?') this way, the code will always prompt at least twice. 这样,代码将始终提示至少两次。 You should either declare userEntree as null , or write the loop as a while loop instead of do-while . 您应该将userEntree声明为null ,或者将循环编写为while循环而不是do-while

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

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