我试图让我的头围绕js中的do / while循环和逻辑运算符。我的问题是将if / else if / else语句嵌入do部分时,因为它不断循环警报。此外,如果我输入除稀有,中等或完成之外的任何内容,它将进入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;
}
答案 0 :(得分:0)
你必须单独进行每次比较,所以
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循环的原因。
答案 1 :(得分:0)
由于你在'中的条件,它会进入无限循环。构造总是等于" true"。
您拥有的内容如下:
while (steak !== "RARE" || "MEDIUM" || "WELL-DONE")
将其更改为
while (steak !== "RARE" && steak !== "MEDIUM" && steak !== "WELL-DONE")
此外,您还有一个类似的问题,其他问题是'声明。
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;
}
如果它仍然无效,请告诉我。
答案 2 :(得分:0)
如上所述,必须单独测试条件,即 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;
}

为我工作。