我需要帮助使我的 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/