为初学者JAVA分配循环

时间:2013-10-09 04:14:06

标签: java loops nested-loops

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

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

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

最后,一旦我们在年底前达到100K佣金,该计划结束,告诉卖家去年度休假。如果现实生活中只有这一点......

然而,我的问题是我无法出于某种原因编程一个循环,它将退出程序(并显示一条消息),如果它发生在12月之后我没有达到100K。我一直在

线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:12     在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
}

}

1 个答案:

答案 0 :(得分: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++ ){