我的循环中有一个问题。我想继续询问用户购买哪种车票类型以及他们想购买多少车票,直到MAX_SEAT_COUNT <= totTickets
。我的代码只能运行一次。 MAX_SEAT_COUNT = 2200
尽管在Javascript中循环
do {
ticketType = prompt ("Ticket Types: Toddlers = 1 Juniors = 2 Adults = 3 Please enter a ticket type: 1, 2, 3", "");
if (ticketType == 1) {
manyToddlers = prompt ("How many toddler tickets are you purchasing? You can only buy 10 tickets per ticket type.", "");
} else if (ticketType == 2) {
manyJuniors = prompt ("How many junior tickets are you purchasing? You can only buy 10 tickets per ticket type.", "");
} else if (ticketType == 3) {
manyAdults = prompt ("How many adult tickets are you purchasing? You can only buy 10 tickets per ticket type.", "");
} else {
alert ("Please enter the correct number for each ticket type.");
}
if (manyToddlers <= 10) {
toddlersCounter = toddlersCounter + manyToddlers;
costToddlers = manyToddlers * toddlers;
alert ("You bought " + manyToddlers + " toddler tickets for $ " + costToddlers.toFixed(2));
} else if (manyJuniors <= 10) {
juniorsCounter = juniorsCounter + manyJuniors;
costJuniors = manyJuniors * juniors;
alert ("You bought " + manyJuniors + " junior tickets for $ " + costJuniors.toFixed(2));
} else if (manyAdults <= 10) {
adultsCounter = adultsCounter + manyAdults;
costAdults = manyAdults * adults;
alert ("You bought " + manyAdults + " adult tickets for $ " + costAdults.toFixed(2));
} else {
alert ("You can only buy 10 tickets per ticket type.");
}
totTickets = toddlersCounter + juniorsCounter + adultsCounter;
totSales = costToddlers + costJuniors + costAdults;
} while (MAX_SEAT_COUNT <= totTickets);
提示命令总是返回一个字符串值,你应该将这些文本数字转换成许多适当的数字byToddlers = parseInt(manyToddlers,10)。数学也可能按预期工作。 – jeff 2014-09-25 06:52:16
难道你没有这种情况吗?当票数小于最大值时,您应该循环,而不是更大。 – Barmar 2014-09-25 06:52:19
循环到2200,每次运行最多10步,并且3个警报真的不推荐:)这将是660警报要做的事情......:S – Dwza 2014-09-25 07:55:29