if elseif 语句在 do/while 语句中无限循环 javascript

if elseif statement in a do/while statement infinite looping javascript

我正在努力研究 do/while js 中的循环和逻辑运算符。我的问题是将 if/else if/else 语句嵌入到 do 部分时,因为它不断循环警报。此外,如果我输入 rare、medium 或 well-done 以外的任何内容,它会进入 else if 语句而不是 else 语句,即使我已经为 else if 语句指定了 medium 或 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();
            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;
}

您必须单独进行每个比较,所以

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 循环的原因。

由于 '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;
}

如果仍然无效,请告诉我。

如上所述,必须单独测试条件,即
如果(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;
}

为我工作。