【问题标题】:Break statement misplaced in while-loop在while循环中放错地方的Break语句
【发布时间】:2017-03-31 08:30:04
【问题描述】:

我想创建一个脚本,从列表(数组)中询问您想要的项目,检查项目是否有库存,如果您输入“列表”,则显示库存并退出程序(while 循环)如果你输入“退出”

while (true) { // endless loop
  var search = prompt('Type the item you are looking for, type "list" to display the inventory, type "quit" to exit te store.');
  search = search.toLowerCase;
}
if (search = 'quit') {
  break; // if the client types quit, they exit the store, loop ends
} else if search == 'list'{
  write('<p>We currently have ' + inventory.join(', ') + ' in stock.</p>');
} else if (inventory.indexOf(search) > - 1) {
  write('<p>Yes, we have ' + search + ' in our stock.</p>');
} else {
  write('<p>Sorry, we don\'t have ' + search + ' in our stock</p>');
}

break 语句不在循环中,但我真的不知道它应该去哪里......

有什么想法吗?

【问题讨论】:

  • 你的开发者工具控制台中的任何错误......沿着SyntaxError: unlabeled break must be inside loop or switch的行......哦,你不会......因为你的代码永远不会因为无休止的while循环而走到那一步
  • also ... = assignment ... == 和 === 相等性测试
  • 整个if 放错了位置。
  • 呃,你为什么不把整个事情放在循环中呢?你不想在再次循环之前对 prompt 结果做点什么吗?

标签: javascript syntax while-loop


【解决方案1】:

只需将整个“if”结构放在“while”循环中!

即,将“}”从第 4 行移到代码末尾。

“break”将与“while”构造相关。

【讨论】:

    【解决方案2】:

    您的代码中至少有三个错误:

    首先 if 在循环之外。 其次,您错过了一个 if 语句周围的括号。 第一个 if 语句中的第三个你错过了第二个等号

    var search='1';
    while (true) { // endless loop
      var search = prompt('Type the item you are looking for, type "list" to display the inventory, type "quit" to exit te store.');
      search = search.toLowerCase;
    
    if (search == 'quit') {
      break; // if the client types quit, they exit the store, loop ends
    } else if (search == 'list'){
      write('<p>We currently have ' + inventory.join(', ') + ' in stock.</p>');
    } else if (inventory.indexOf(search) > - 1) {
      write('<p>Yes, we have ' + search + ' in our stock.</p>');
    } else {
      write('<p>Sorry, we don\'t have ' + search + ' in our stock</p>');
    }
    }

    【讨论】:

      【解决方案3】:

      试试这个。您在 if 语句中放错了括号,也错过了 ==;

      while (true) { // endless loop
        var search = prompt('Type the item you are looking for, type "list" to display the inventory, type "quit" to exit te store.');
        search = search.toLowerCase;
      
      if (search == 'quit') {
        break; // if the client types quit, they exit the store, loop ends
      } else if search == 'list'{
        write('<p>We currently have ' + inventory.join(', ') + ' in stock.</p>');
      } else if (inventory.indexOf(search) > - 1) {
        write('<p>Yes, we have ' + search + ' in our stock.</p>');
      } else {
        write('<p>Sorry, we don\'t have ' + search + ' in our stock</p>');
      }
      }

      【讨论】:

        【解决方案4】:

        您的代码中有几个错误,有些是语法错误,例如缺少括号,或者只使用了一个等号。 break 关键字必须在循环内,否则将没有循环可以跳出。

        但是,如果不是我们说if(!search) continue; 的部分,而是说if(!search) break;,您可以通过单击取消或输入一个空字符串来跳出循环,如果您问我这更有意义。

        var inventory = ['tea','sugar'];
        while (true) { // endless loop
          var search = prompt('Type the item you are looking for, type "list" to display the inventory, type "quit" to exit te store.');
        
          if(!search) continue;
        
          search = search.toLowerCase();
        
          if (search == 'quit') {
            break; // if the client types quit, they exit the store, loop ends
          } else if (search == 'list') {
            alert('<p>We currently have ' + inventory.join(', ') + ' in stock.</p>');
          } else if (inventory.indexOf(search) > -1) {
            alert('<p>Yes, we have ' + search + ' in our stock.</p>');
          } else {
            alert('<p>Sorry, we don\'t have ' + search + ' in our stock</p>');
          }
        }

        【讨论】:

          【解决方案5】:

          您应该在 while 循环中检查您的数据,并在需要时将其中断。

          我还更正了错误并测试了脚本。

          您可以在我的 cmets 中找到有关错误的详细信息。

          while (true) { // endless loop
            var search = prompt('Type the item you are looking for, type "list" to display the inventory, type "quit" to exit te store.');
            search = search.toLowerCase();
                // After getting user input you want to check it and decide accordingly if
            // you want to stop the loop or you want to ask the user for input again
            if (search == 'quit') { // *** ERROR *** In your code you used a
          // single = and that doesn't work for comparison in Javascript
              break; // if the client types quit, they exit the store, loop ends
            } else if (search == 'list') {// *** ERROR *** If you have more than a single if you need to always use parenthesis
              console.log('<p>We currently have ' + inventory.join(', ') + ' in stock.</p>');
            } else if (inventory.indexOf(search) > -1) {
              console.log('<p>Yes, we have ' + search + ' in our stock.</p>');
            } else {
              console.log('<p>Sorry, we don\'t have ' + search + ' in our stock</p>');
            }
          
          }

          【讨论】:

            猜你喜欢
            • 2013-11-17
            • 1970-01-01
            • 1970-01-01
            • 2020-12-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-05-10
            相关资源
            最近更新 更多