【问题标题】:if elseif statement in a do/while statement infinite looping javascriptdo / while语句中的if elseif语句无限循环javascript
【发布时间】:2016-04-06 17:06:57
【问题描述】:

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

【问题讨论】:

  • if (steak === "MEDIUM"||"WELL-DONE") 这样的语法在 JavaScript 中是行不通的。您必须单独进行每个比较。例如。 if (steak === "MEDIUM" || steak === "WELL-DONE")
  • @j08691 已经指出了您需要如何进行比较。无限循环的原因是"any string" 是真实的,即。比较与while (steak !== "RARE" || true || true) 相同,这始终是正确的。

标签: javascript infinite-loop do-while


【解决方案1】:

由于您在“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;
}

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      为我工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-07
        • 1970-01-01
        • 2023-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多