break语句在while循环中放错位置

时间:2017-03-31 08:30:05

标签: javascript syntax while-loop

我想创建一个脚本,从列表(数组)中询问您想要的项目,检查项目是否有库存,如果您的类型“列表”显示库存并退出程序(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语句不在循环中,但我真的不知道它应该去哪里......

有什么想法吗?

5 个答案:

答案 0 :(得分:0)

只要把整个&#34; if&#34;在&#34;内部构建&#34;而#34;循环!

即,移动&#34;}&#34;从第4行到代码的结尾。

&#34;休息&#34;然后将与&#34; while&#34;构造

答案 1 :(得分:0)

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

首先,if在Loop之外。 第二个你错过括号arround一个如果声明。 第一个if语句中的第三个你错过了第二个等号

&#13;
&#13;
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>');
}
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

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

&#13;
&#13;
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>');
}
}
&#13;
&#13;
&#13;

答案 3 :(得分:0)

如果需要,您应该在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();
      // 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>');
  }

}

答案 4 :(得分:0)

您的代码中存在多个错误,一些语法错误,例如缺少括号,或仅使用一个等号。 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>');
  }
}

相关问题