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

Making continue statement work in a do while loop

我需要帮助使我的 continue 语句在 do-while 循环中工作。如果一天中的时间无效,我想忽略卡片的数量,只转到循环的下一次迭代。 现在,如果我不为 'time' 输入 'Morning'、'Afternoon' 或 'Evening',则循环结束。

这是我的代码:

// 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:这是我的电脑 class。这是我们的 activity: "In this assignment you will be pseudo-coding an algorithm that accumulates customer survey cards by time of day. You will need to ask for input from the user for time of day (morning, afternoon, and evening) and the number of cards in that batch. The user needs to be able to enter multiple batches for the same time of the day. You will need to use separate accumulators in your loop to keep track of the cards as the loop continues its iterations."

问题是您的 while 条件不合格。如果用户输入的是非值输入,则继续会被命中,您的代码将跳转以评估条件 while (repeat == 'Y')

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

因为null != 'Y'你的循环结束