java - 我需要有人告诉我代码中的逻辑有什么问题

标签 java

我是Java初学者。我正在尝试根据以下文本编写代码:

You are to write an application to pre­sell 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) - Requests ticketRequest number of tickets. Returns TicketSeller.TOO_MANY_TICKETS_REQUESTED if there are more than TicketSeller.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/

相关文章:

src/main/resources 中文件的 Java 路径

java - startActivity() 不启动 Activity

java - 可以避免 google 端点 URL 中的版本号

java - 如何使用 Java EE 7 和 Glassfish 4 在 IntelliJ IDEA Community Edition 中创建 Java Servlet 应用程序?

java - META-INF/versions/9/* 和带有 Jetty 9.2 的 Elasticsearch 6.2.2+ 上的字节码扫描错误

java - 静态-非静态交互

java - 使用 Java(或不使用)将 .xls 转换为 .pdf

java - 如果Java中HashMap键相等,如何取2个最大值

java - 如何从IDE远程上传并执行java程序

java - double 被四舍五入而不精确