编码java帮助ticketmaster

时间:2014-04-05 00:53:01

标签: java

如何才能让客户数量等于购买的门票数量?因此,购买的每张门票都等于一个客户。我无法弄清楚是否已经开始工作一段时间但是如果有人能提供帮助就不能理解这一点非常感谢。

Scanner in = new Scanner(System.in);
int balanceTicket = 100;
int desiredTicketAmount = 0;
int buyers = 0;

 while (balanceTicket > 0) {
        System.out.println("please enter the number of tickets wanted");
        desiredTicketAmount = in.nextInt();

        // check if entered integer is negative
        if (desiredTicketAmount < 0) {
            System.out.println("please enter a positive number.");
            System.out.println();  
        } else if (desiredTicketAmount > 4) {
            System.out.println("please enter a number less than 4 ");
            System.out.println();
        } else if (desiredTicketAmount <= 4) {
            // check if the entered amount is available in your pool
            if (balanceTicket - desiredTicketAmount < 0) {  
                System.out.println("sorry, we only have " + balanceTicket + " tickets left.");
                System.out.println();
            } else {
                buyers++;
                balanceTicket = balanceTicket - desiredTicketAmount;
                System.out.println(" the number of tickes left are : " + balanceTicket);
                System.out.println();
                System.out.println(" the  total number of customers: " + buyers);
                System.out.println();
                System.out.println();
            }
          }
        } 
    }
}

2 个答案:

答案 0 :(得分:0)

而不是:

buyers++;

尝试按照购买的门票数量递增buyers。像这样。

buyers += desiredTicketAmount;

答案 1 :(得分:0)

在我看来,好像你不理解你的任务。

我为什么这么说?

  • 因为买家可以购买多张票。
  • 说买家等于购买的每张门票都没有意义。

所以... 使用int变量买家对买家来说等于购买门票的数量是不好的做法。

取代那个案例的买家。如何用买方替换下面的变量。

int ticketsPurchased;