我是Java初学者。我正在尝试根据以下文本编写代码:
You are to write an application to presell a limited number of movie tickets. The simple user interface will be contained in Main.java and the object to implement this is referred to as TicketSeller and consists of only the following public methods:
public TicketSeller(int initialTicketAllotment)
- Specify the number of tickets you want to sell.
public int requestTickets(int ticketRequest)
- RequeststicketRequest
number of tickets. Returns TicketSeller.TOO_MANY_TICKETS_REQUESTED
if there are more thanTicketSeller.MAXIMUM_TICKETS_ALLOWED
. Note that one of these constants is private and one is public. Which one needs to be exposed to the user?
public int getNumberOfBuyers()
- Tracks number of ticket buyers. You only count buyers who have fulfilled a request. Errors don't count.
这是我到目前为止所拥有的。但是,我的代码中有一个错误,但我找不到它:
public class TicketSeller {
private final static int MAXIMUM_TICKETS_ALLOWED = 4;
public static final int TOO_MANY_TICKETS_REQUESTED = -1;
private int buyers = 0;
private int initialTicketAllotment;
int placeHolder;
public TicketSeller(int initialTicketAllotment) {
this.initialTicketAllotment = initialTicketAllotment;
}
public int requestTickets(int ticketRequest) {
if (placeHolder > 0) {
int value = 0;
value = placeHolder - ticketRequest;
placeHolder = value;
buyers += 1;
}
if (ticketRequest > MAXIMUM_TICKETS_ALLOWED
|| ticketRequest > initialTicketAllotment) {
return TOO_MANY_TICKETS_REQUESTED;
}
if (ticketRequest >= 0 && ticketRequest <= MAXIMUM_TICKETS_ALLOWED
&& placeHolder == 0) {
int value = 0;
value = initialTicketAllotment - ticketRequest;
placeHolder = value;
buyers += 1;
}
return placeHolder;
}
public int getNumberOfBuyers() {
return buyers;
}
}
以下代码注释中的输出不正确:
public class Main {
public static void main(String[] args) {
TicketSeller ts = new TicketSeller(8);
System.out.println(ts.requestTickets(1)); //outputs 7
System.out.println(ts.requestTickets(2)); //outputs 5
System.out.println(ts.requestTickets(3)); //outputs 2
System.out.println(ts.requestTickets(1)); //outputs 1
System.out.println(ts.requestTickets(1)); //outputs 7, instead of 0
}
}
最佳答案
您的逻辑错误在于您的构造函数和您的 request Tickets(in)
方法:
public TicketSeller(int initialTicketAllotment) {
this.initialTicketAllotment = initialTicketAllotment;
// you need to initialize 'placeholder' to the initial
// amount of tickets available
this.placeholder = initialTicketAllotment;
}
public int requestTickets(int ticketRequest) {
// you should check if there are no more tickets to request
// or more than MAXIMUM_TICKETS_ALLOWED (4 tickets) have been
// requested, before checking if placeholder > 0. Also, you
// want to check if 'ticketRequest > placeholder' not the
// initial amount of tickets.
if (ticketRequest > MAXIMUM_TICKETS_ALLOWED
|| ticketRequest > placeholder) {
return TOO_MANY_TICKETS_REQUESTED;
}
// remove the local 'value' variable, it just confuses your
// code to other readers, thinking you need a temporary variable
// for some reason
if (placeHolder > 0) {
placeholder -= ticketRequest;
buyers += 1;
}
// if placeholder equals 0, that means there are no more tickets
// left... not sure why you would set it back to
// 'initialTicketAllotment - ticketRequest'. I would get rid of this
// method.
/*
if (ticketRequest >= 0 && ticketRequest <= MAXIMUM_TICKETS_ALLOWED
&& placeHolder == 0) {
int value = 0;
value = initialTicketAllotment - ticketRequest;
placeHolder = value;
buyers += 1;
}
*/
return placeHolder;
}
关于java - 我需要有人告诉我代码中的逻辑有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33458571/