我正试图使js中的do/while循环和逻辑运算符保持清醒。我的问题是在do部分中嵌入if/ else if/ else语句时,它一直在循环警报。另外,如果我键入的不是罕见的、中等的或做得好的,那么它会进入else if语句,而不是else语句,即使我已经为else if语句指定了中介语句或做得好的语句。我哪里出问题了?任何帮助都将不胜感激。
谢谢。
var food;
var steak;
food = prompt("Hey what type of food do you want to eat? We have steak, chicken, legumes, kangaroo or pufferfish?","Select your choice here.").toUpperCase();
switch (food) {
case 'STEAK':
alert ("Sure no problem, how would you like your steak done?");
steak = prompt("Would you like your steak done rare, medium or well-done?").toUpperCase();
do {
if (steak === "RARE") {
alert ("You eat the rare steak");
}
else if (steak === "MEDIUM"||"WELL-DONE") {
alert("You eat your steak and it's good.");
}
else {
alert("Sorry didn't catch that.");
steak = prompt("Choose rare, medium or well-done.");
}
} while (steak !== "RARE" || "MEDIUM" || "WELL-DONE");
break;
}发布于 2016-04-06 17:34:05
它是无限循环的,因为'while‘结构中的条件总是等同于"true“。
你所拥有的如下:
while (steak !== "RARE" || "MEDIUM" || "WELL-DONE")把它改成
while (steak !== "RARE" && steak !== "MEDIUM" && steak !== "WELL-DONE")另外,在其中一个“else if”语句中也有类似的问题。
else if (steak === "MEDIUM"||"WELL-DONE") {把它改成
else if (steak === "MEDIUM"|| steak === "WELL-DONE") {在您的脚本中仍然存在一些逻辑问题。试一试以下几种方法:
var food;
var steak;
food = prompt("Hey what type of food do you want to eat? We have steak, chicken, legumes, kangaroo or pufferfish?","Select your choice here.").toUpperCase();
switch (food) {
case 'STEAK':
alert ("Sure no problem, how would you like your steak done?");
steak = prompt("Would you like your steak done rare, medium or well-done?").toUpperCase();
while (steak !== "RARE" && steak !== "MEDIUM" && steak !== "WELL-DONE") {
alert("Sorry didn't catch that.");
steak = prompt("Choose rare, medium or well-done.").toUpperCase();
}
if (steak === "RARE") {
alert ("You eat the rare steak");
}
else if (steak === "MEDIUM"|| steak === "WELL-DONE") {
alert("You eat your steak and it's good.");
}
break;
}如果还不起作用的话请告诉我。
发布于 2016-04-06 17:19:40
你必须逐个做比较,所以
else if (steak === "MEDIUM"||"WELL-DONE") {
alert("You eat your steak and it's good.");
}应该是
else if (steak === "MEDIUM"|| steak === "WELL-DONE") {
alert("You eat your steak and it's good.");
}诸若此类。这也是无限do/while循环的原因。
发布于 2016-04-06 17:54:24
如前所述,这些条件必须单独进行测试,即
if (a == n || a == m)还有你最后的说法不对。你可能会想做这样的事
var food;
var steak;
food = prompt("Hey what type of food do you want to eat? We have steak, chicken, legumes, kangaroo or pufferfish?","Select your choice here.").toUpperCase();
switch (food) {
case 'STEAK':
alert ("Sure no problem, how would you like your steak done?");
steak = prompt("Would you like your steak done rare, medium or well-done?").toUpperCase();
console.log('steak', steak);
do {
console.log('steak inside do', steak);
if (steak === "RARE") {
alert ("You eat the rare steak");
}
else if (steak === "MEDIUM"|| steak ==="WELL-DONE") {
alert("You eat your steak and it's good.");
}
else {
alert("Sorry didn't catch that.");
steak = prompt("Choose rare, medium or well-done.").toUpperCase();
}
} while (steak !== "RARE" && steak !== "MEDIUM" && steak !== "WELL-DONE");
break;
}
为我工作过。
https://stackoverflow.com/questions/36457797
复制相似问题