首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果do/while语句无限循环javascript中的其他语句

问如果do/while语句无限循环javascript中的其他语句
EN

Stack Overflow用户
提问于 2016-04-06 17:06:57
回答 3查看 646关注 0票数 0

我正试图使js中的do/while循环和逻辑运算符保持清醒。我的问题是在do部分中嵌入if/ else if/ else语句时,它一直在循环警报。另外,如果我键入的不是罕见的、中等的或做得好的,那么它会进入else if语句,而不是else语句,即使我已经为else if语句指定了中介语句或做得好的语句。我哪里出问题了?任何帮助都将不胜感激。

谢谢。

代码语言:javascript
复制
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;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-06 17:34:05

它是无限循环的,因为'while‘结构中的条件总是等同于"true“。

你所拥有的如下:

代码语言:javascript
复制
while (steak !== "RARE" || "MEDIUM" || "WELL-DONE")

把它改成

代码语言:javascript
复制
while (steak !== "RARE" && steak !== "MEDIUM" && steak !== "WELL-DONE")

另外,在其中一个“else if”语句中也有类似的问题。

代码语言:javascript
复制
else if (steak === "MEDIUM"||"WELL-DONE") {

把它改成

代码语言:javascript
复制
else if (steak === "MEDIUM"|| steak === "WELL-DONE") {

在您的脚本中仍然存在一些逻辑问题。试一试以下几种方法:

代码语言:javascript
复制
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;
}

如果还不起作用的话请告诉我。

票数 0
EN

Stack Overflow用户

发布于 2016-04-06 17:19:40

你必须逐个做比较,所以

代码语言:javascript
复制
else if (steak === "MEDIUM"||"WELL-DONE") {
    alert("You eat your steak and it's good.");
}

应该是

代码语言:javascript
复制
else if (steak === "MEDIUM"|| steak === "WELL-DONE") {
    alert("You eat your steak and it's good.");
}

诸若此类。这也是无限do/while循环的原因。

票数 0
EN

Stack Overflow用户

发布于 2016-04-06 17:54:24

如前所述,这些条件必须单独进行测试,即

代码语言:javascript
复制
 if (a == n || a == m)

还有你最后的说法不对。你可能会想做这样的事

​

代码语言:javascript
复制
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;
}

​

为我工作过。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36457797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档