使while语句在do while循环中起作用

时间:2017-04-07 20:15:26

标签: 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:这是我的电脑课。这是我们的活动: “在此作业中,您将伪编码一种算法,该算法可以按时间累积客户调查卡。您需要向用户询问一天中的时间(早上,下午和晚上)以及卡中的卡数。用户需要能够在一天中的同一时间输入多个批次。当循环继续迭代时,您需要在循环中使用单独的累加器来跟踪卡片。“

1 个答案:

答案 0 :(得分:1)

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

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

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