我的计划的目标是:
编写申请预售有限数量的私人音乐会门票。每位买家可以购买多达6张门票。不超过75张门票可以出售。实施一个程序,提示用户输入所需数量的票证,然后显示剩余票证的数量。在您的实现中使用while循环。重复直到售出所有门票,然后显示买家总数。
我的循环关闭似乎有问题,因为即使循环变量达到其条件限制,它仍然会提示票数。知道为什么吗?
import java.util.Scanner;
public class TicketCounterHorn {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int ticketsToBuy;
int totalTickets =75;
int buyers = 0;
int ticketMax = 6;
while (totalTickets >= 0) {
System.out.print("How many tickets would you like to purchase: ");
ticketsToBuy = in.nextInt();
if (ticketsToBuy <= ticketMax && ticketsToBuy > 0) {
if (ticketsToBuy < totalTickets) {
totalTickets = totalTickets - ticketsToBuy;
buyers++;
System.out.println(ticketsToBuy);
System.out.println(buyers);
System.out.println(totalTickets);
}
}
}
System.out.println("the total number of buyers is: " + buyers);
}
}
答案 0 :(得分:1)
条件
function Card(number, suit) {
this.number = number;
this.suit = suit;
}
var deck = [];
function Deck() {
for (i=2; i>13; i++) {
for (j=1; j>4; j++) {
var newCard = new Card(i,j);
deck.push(newCard);
}
}
return deck
}
var newDeck = new Deck();
console.log(newDeck.length);
也应该是
if (ticketsToBuy < totalTickets)
答案 1 :(得分:0)
我认为循环条件应该改为:
while (totalTickets > 0)
否则,即使没有更多可卖的东西,你也会继续销售......
<强>更新强>
正如 David Conrad 在评论中提到的,还有一个问题:
还有第二个问题。测试
if (ticketsToBuy < totalTickets)
必须更改为if (ticketsToBuy <= totalTickets)
,否则请更改为let s : Int32 = 4 + var // var is Int32 somewhere else msg.substringToIndex(msg.startIndex.advancedBy(s)
不能买最后一张票
谢谢,大卫,提起这件事!