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



我正试图了解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;
}

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

如果它仍然不起作用,请告诉我。

您必须单独进行每个比较,因此

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

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

为我工作。

最新更新