javascript - 使 continue 语句在 do while 循环中工作

标签 javascript loops do-while continue

我需要帮助使我的 continue 语句在 do-while 循环中工作。如果一天中的时间无效,我想忽略卡片的数量,然后转到循环的下一次迭代。 现在,如果我不输入“上午”、“下午”或“晚上”作为“时间”,循环就会结束。

这是我的代码:

// DECLARE VARIABLES
var time; // timestamp on batch
var count; // number of cards in batch
var repeat; // whether or not the program will repeat
var m = 0; // initial number of Morning cards
var a = 0; // initial number of Afternoon cards
var e = 0; // initial number of Evening cards
var total = 0; // initial number of all cards

//START LOOP
do {
  time = prompt("Is the batch's timestamp 'Morning', 'Afternoon' or 'Evening'?"); // Input value for time
  count = prompt("How many cards are in this batch?"); // Input value for count
  count = parseFloat(count); // return 'count' as a number

  total = total + count; // calculate total number of cards

  if (time == "Morning") {
    m = m + count; // if time is 'Morning', add the # of the cards from this batch to the # of cards from all 'Morning' batches
  } else if (time == "Afternoon") {
    a = a + count; // if time is 'Afternoon', add the # of the cards from this batch to the # of cards from all 'Afternoon' batches
  } else if (time == "Evening") {
    e = e + count; // if time is 'Evening', add the # of the cards from this batch to the # of cards from all 'Evening' batches
  } else {
    continue;
  }

  repeat = prompt("Do you have more batches to enter? Enter 'Y' or 'N'"); // User chooses whether to end the loop
} while (repeat == "Y");

// DISPLAY RESULTS
document.write("Total number of cards: " + total + "<br/>");
document.write("Morning cards: " + m + "<br/>");
document.write("Afternoon cards: " + a + "<br/>");
document.write("Evening cards: " + e);

PS:这是我的计算机课。这是我们的事件: “在本作业中,您将对一种算法进行伪编码,该算法按一天中的时间累积客户调查卡。您需要要求用户输入一天中的时间(早上、下午和晚上)以及卡片的数量。该批处理。用户需要能够在一天中的同一时间输入多个批处理。您将需要在循环中使用单独的累加器来在循环继续迭代时跟踪卡片。”

最佳答案

问题是您的 while 条件失败。如果用户输入非值输入,则继续会被命中,并且您的代码会向下跳转以评估条件 while (repeat == 'Y')

此时,repeat 等于 null,因为从未调用过更改它的提示。

由于 null != 'Y' 你的循环结束

关于javascript - 使 continue 语句在 do while 循环中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43286432/

相关文章:

c - 如何让 C 查看文件并找到某个字符串?

javascript - 从回调函数中返回 JSON 数据

javascript - 大表单或 javascript 锁定浏览器

loops - 仅使用 ffmpeg,是否可以使用源 GIF 图像并通过输入 GIF 输出设定长度 > 一个循环的视频?

c - 我的循环不会检查 C 中数组的所有元素

javascript - 为什么使用 do-while 而不是简单的 if 语句

javascript - 计算循环执行了多少次

具有图像上传功能的 Javascript(或可能不是)文本编辑器

javascript - 当前页面上的行数?

python - 在列表中生成字符串元素的组合,将它们限制为特定的字符数量