0

我有一个初学者 JAVA 课程的作业,我似乎无法解决它。我们需要创建一个基于交易计算每月佣金的程序。交易金额使用 math.random() 定义。

这个月从一月开始。我们使用 JOPtionPane.showconfirmdialog 来询问是否有客户。如果是,则有另一个确认对话框询问客户是否希望购买该物品。如果客户接受购买该商品,我们将计算佣金。

如果没有客户或者我们在一个月内达到 15K 的佣金,我们会跳到下个月并重复。

最后,一旦我们在年底前达到 100K 的佣金,程序就会结束,告诉卖家今年剩下的时间去度假。如果这在现实生活中是真的……

但是,我的问题是,如果我在 12 月之后没有达到 100K,由于某种原因,我无法编写一个将退出程序(并显示一条消息)的循环。我不断得到

线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 12 at Commission.main(Commission.java:42)

public static void main(String[] args) {

    String[] months = { "January", "February", "March", "April",
            "May", // Initialize array for months of the year
            "June", "July", "August", "September", "October", "November",
            "December" };
    String[] diamonds = { "diamond", "ruby", "sapphire", "emerald",
            "topaz", "zircon" }; // Initialize array for different precious gems types
    double[] monthlyCommission; // Initialize double variable for total commission for the month
    monthlyCommission = new double[12]; // Initialize array with 12 values for monthly commission
    double yearTotal; // Initialize variable for total commission in the year
    double transaction; // Initialize value for a sale transaction
    double commission; // Initialize value for commission based on value of sale transaction
    int month = 0;
    int diamond;
    yearTotal = 0;

    JOptionPane.showMessageDialog(null, "Welcome to X's Jewelry Store!"); // Display welcome message dialog

    for (; monthlyCommission[month] < 15000;) {

        int storeCustomer = JOptionPane.showConfirmDialog(null,
                "Is there a customer in the store?", months[month]
                        + " month", JOptionPane.YES_NO_OPTION);
        while (storeCustomer == JOptionPane.NO_OPTION) { // Statement to process if there is no customer
            {
                month += 1;
                storeCustomer = JOptionPane.showConfirmDialog(null,
                        "Is there a customer in the store?", months[month]
                                + " month", JOptionPane.YES_NO_OPTION);
            }

        }
        if (storeCustomer == JOptionPane.YES_OPTION) { // Statement to process if there is a customer

            diamond = (int) (Math.random() * 6); // Choose randomly a value between 0-5
            transaction = Math.random() * 50000.0;
            transaction = Math.round(transaction * 100) / 100;

            int buyItem = JOptionPane.showConfirmDialog(null,
                    "Do you wish to buy this " + diamonds[diamond]
                            + " for "
                            + String.format("$%4.2f", transaction) + "?");
            if (buyItem == JOptionPane.NO_OPTION) // Statement to process if user does not want the item
                JOptionPane.showMessageDialog(null,
                        "No problem. See you next time.");

            if (buyItem == JOptionPane.YES_OPTION) { // Statement to process if user wishes to buy the item
                if (transaction <= 10000)
                    commission = transaction * 0.1;
                else if (transaction > 30000.0)
                    commission = 10000 * 0.10 + 20000 * 0.15
                            + (transaction - 30000) * 0.20;
                else
                    commission = 10000 * 0.10 + (transaction - 10000) * 0.15;

                commission = Math.round(commission * 100) / 100;
                monthlyCommission[month] += commission;
                yearTotal += commission;

                JOptionPane.showMessageDialog(null, String.format(
                        "Your commission for this transaction is $%4.2f",
                        commission));
                System.out.println(yearTotal); // Displays the commission total for the transaction
                if (yearTotal > 100000) // Exit loop if total commission for the year is greater than 100000
                    break;

            }

        }

        if (monthlyCommission[month] >= 15000) {
            JOptionPane.showMessageDialog(null, "You have earned $"
                    + String.format("%4.2f", monthlyCommission[month])
                    + ". You can rest the remainder of the month!"); // Display dialog once the monthly commission reaches 15000
            month += 1;
        }

    }
    JOptionPane.showMessageDialog(null,
            "Congratulations!! You have earned a total of $"
                    + String.format("%4.2f", yearTotal)
                    + ". Enjoy your vacation in Honolulu!"); // Display dialog once the yearly commission reaches 100000
}

}

4

1 回答 1

1

我相信你的问题在于你开始的“for”声明。您的退出条件是当您的月佣金达到超过 15000 时,无论是几月。如果您的佣金已达到 15000(第 72-77 行),您已经增加了月份,因此您的 for 循环应该只遍历 0-11 月份。因此第 20 行应该看起来更像:

    for( month = 0; month < 12; month++ ){

另外,第 66 行的中断有点难看,我可以建议将该条件也移到 for 循环中吗?像这样:

    for( month= 0; ( (month < 12) && (yearTotal<100000) ) ; month++ ){
于 2013-10-09T04:29:25.863 回答