gpt4 book ai didi

javascript - 在 JavaScript 中执行 While 循环

转载 作者:行者123 更新时间:2023-11-28 19:35:04 25 4
gpt4 key购买 nike

我的循环有问题。我想一直询问用户要购买哪种门票类型以及他们想购买多少张,直到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com