java - 初学者 JAVA 作业中的循环

标签 java loops nested-loops

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

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

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

最后,一旦我们在年底前佣金达到 10 万,该计划就会结束,并告诉卖家在今年剩下的时间里去度假。如果这在现实生活中是真的就好了……

但是,我的问题是,由于某种原因,如果我在 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
}

}

最佳答案

我相信您的问题在于您开头的“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++ ){

关于java - 初学者 JAVA 作业中的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19262991/

相关文章:

java - Keycloak Testcontainer 由于等待 URL 可访问超时而失败 (http ://localhost:55127/auth should return HTTP 200)

java - 绑定(bind)到线程 [main] 的键 [org.hibernate.internal.SessionFactoryImpl] 没有值

c - C 中输入整数的特定位数计数

java - 嵌套循环模式

c++ - 将循环系统转换为单个方程

c++ - 创建 N 嵌套 for 循环

java - java字符串中equals和compareto方法的区别

java - 最终字段名称的 Getter 方法

javascript - 一个案例有多个中断是不好的做法吗?

arrays - 用于检测n个整数的未排序数组中整数重复的算法。 (通过2个嵌套循环实现)