作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的循环有问题。我想一直询问用户要购买哪种门票类型以及他们想购买多少张,直到 MAX_SEAT_COUNT <= totTickets
。我的代码只会运行一次问题。 MAX_SEAT_COUNT = 2200
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);
最佳答案
你的while
条件是错误的。应该是:
while (totTickets <= MAX_SEAT_COUNT)
循环仅运行一次,因为您的条件在第一个循环后失败。
关于javascript - 在 JavaScript 中执行 While 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26032308/
我是一名优秀的程序员,十分优秀!