【问题标题】:Java issue using classes and accessing variables使用类和访问变量的 Java 问题
【发布时间】:2017-08-04 09:26:06
【问题描述】:

我目前正在学习 Java 课程。我们的任务是编写一个程序,在该程序中,总年薪是通过固定年薪、当年销售额的输入以及作为佣金赚取的销售额的百分比来计算的。我的问题是我应该使用哪个变量来获得表中 Total Compensation 列的正确输出。我无法弄清楚如何使用另一列中的数字并利用其他类的逻辑来输出正确的数字。到目前为止,这是我的代码,每个类都是一个单独的 Java 文件:

package sales;

import java.util.Scanner;

public class Sales {
public static void main(String[] args) {
    // Initialise scanner
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter your annual sales:");

    // Read sales from input & calculate salary
    double annualSales = input.nextDouble();
    double salary = Utils.calculateSalary(annualSales);

    // Calculate commission bonus
    double commissionBonus = 1.5 * annualSales;

    // Print information for user
    System.out.println("The total yearly salary is: " + Utils.numFormat(salary));
    System.out.println("Total Sales \t\t Total Compensation");

    while (annualSales <= commissionBonus) {
        System.out.println(annualSales + " \t\t " + salary);
        annualSales += 5000;

        // Update salary according to annual sales
        salary = Utils.calculateSalary(annualSales);
    }

    // Close scanner
    input.close();
}
}

package sales;

/**
 *
 * @author etw11
 */
import java.text.DecimalFormat;

public class Utils {
     public final static double FIXED_SALARY = 30000;
/**
 * @param dec
 * @return 
 */
public static String numFormat(double dec) {
    return new DecimalFormat("##,##0.00").format(dec);
}

/**
 * Calculates the salary based on the given sales.
 * @param sales The annual sales
 * @return The calculated salary.
 */
public static double calculateSalary(double sales) {
    double commissionRate = 0.10d;

    if (sales < 320000) {
        commissionRate = 0.00d;
    } else if (sales >= 320000 && sales < 400000) {
        commissionRate = 0.08d;
    }

    // System.out.println("The current commission is " + (int)(commissionRate * 100) + "% of total sales.");

    return FIXED_SALARY + (sales * commissionRate);
}
}

【问题讨论】:

    标签: java class methods while-loop


    【解决方案1】:

    不确定,如果这是您想要的,但您需要在循环内更新您的 total 以更新 Total Compensation 列中的值。

    让我们从你应该首先改变的小事开始。 Java 中的 naming convention 是驼峰式的。你应该相应地改变它。例如,您应该将函数命名为 getSalesDouble 而不是 getsalesDouble - 类似于您的 numFormat

    接下来,您还应该更改方法和参数/变量的名称。为了做到这一点,我想问自己这个方法要做什么?。一旦我知道我会为它想出一个合适的动词,这就是我的方法的名称。例如,您的 Calculator 应命名为 calculate 以便拥有动词。但这不是不言自明的,因此,必须用另一个名称来代替它——问问自己它是怎么计算的?(例如calculateSalary)。

    接下来,您的 utilsCalculate 类应该以大写字母开头(参见上面的链接)。我更愿意将其命名为Utils,因为无论如何只有一个 utils 类。此外,该类应该只有静态方法,因此您不需要创建实例(参见下面的代码)。您可能希望创建一个私有默认构造函数,以避免团队中的其他程序员意外创建Utils 类的实例。

    现在,话虽如此,你应该避免像你一样发表评论。它只会使您的代码混乱。例如,方法的结束由} 指示。所以,没有必要说明方法的结尾是在} 出现的那一行。如果您发现很难看到方法的结束 } 在哪里,您可以考虑正确格式化您的代码。通常,您的 IDE 可以通过按下快捷方式或使用菜单栏为您完成此操作。

    最后但并非最不重要的一点是,我认为您的代码适用于您,但 total 不会更新(在您的 while loop 内)。这是因为您只计算了总数一次,并且永远不会用salesDouble 的新值重新计算它。在更新 salesDouble 值后,您必须调用 total = salesObject.Calculator();。当然,这需要您创建一个新的utilsCalculate 实例。否则,该值将始终相同,因为您永远不会在 utils 类中更新 salesDouble。我不建议您这样做,但可以按原样解决您的代码问题。

    有这个例子,关于如何做到这一点正确*.

    实用程序

    package utils;
    
    import java.text.DecimalFormat;
    
    public class Utils {
        public final static double FIXED_SALARY = 30000;
    
        /**
         * Formats the given decimal number.
         * @param dec The number to format
         * @return The formatted number as string.
         */
        public static String numFormat(double dec) {
            return new DecimalFormat("##,##0.00").format(dec);
        }
    
        /**
         * Calculates the salary based on the given sales.
         * @param sales The annual sales
         * @return The calculated salary.
         */
        public static double calculateSalary(double sales) {
            double commissionRate = 0.10d;
    
            if (sales < 320000) {
                commissionRate = 0.00d;
            } else if (sales >= 320000 && sales < 400000) {
                commissionRate = 0.08d;
            }
    
            // System.out.println("The current commission is " + (int)(commissionRate * 100) + "% of total sales.");
    
            return FIXED_SALARY + (sales * commissionRate);
        }
    }
    

    主要

    package sales;
    
    import java.util.Scanner;
    
    import utils.Utils;
    
    public class Person {
        public static void main(String[] args) {
            // Initialise scanner
            Scanner input = new Scanner(System.in);
            System.out.print("Please enter your annual sales:");
    
            // Read sales from input & calculate salary
            double annualSales = input.nextDouble();
            double salary = Utils.calculateSalary(annualSales);
    
            // Calculate commission bonus
            double commissionBonus = 1.5 * annualSales;
    
            // Print information for user
            System.out.println("The total yearly salary is: " + Utils.numFormat(salary));
            System.out.println("Total Sales \t\t Total Compensation");
    
            while (annualSales <= commissionBonus) {
                System.out.println(annualSales + " \t\t " + salary);
                annualSales += 5000;
    
                // Update salary according to annual sales
                salary = Utils.calculateSalary(annualSales);
            }
    
            // Close scanner
            input.close();
        }
    }
    

    输出

    Please enter your annual sales:320000
    The total yearly salary is: 55.600,00
    Total Sales          Total Compensation
    320000.0         55600.0
    325000.0         56000.0
    330000.0         56400.0
    335000.0         56800.0
    340000.0         57200.0
    345000.0         57600.0
    350000.0         58000.0
    355000.0         58400.0
    360000.0         58800.0
    365000.0         59200.0
    370000.0         59600.0
    375000.0         60000.0
    380000.0         60400.0
    385000.0         60800.0
    390000.0         61200.0
    395000.0         61600.0
    400000.0         70000.0
    405000.0         70500.0
    410000.0         71000.0
    415000.0         71500.0
    420000.0         72000.0
    425000.0         72500.0
    430000.0         73000.0
    435000.0         73500.0
    440000.0         74000.0
    445000.0         74500.0
    450000.0         75000.0
    455000.0         75500.0
    460000.0         76000.0
    465000.0         76500.0
    470000.0         77000.0
    475000.0         77500.0
    480000.0         78000.0
    

    * 当然,还有提升的空间,不过我觉得应该足够解决你的问题了。

    注意我没有检查你的方程式,因为我不知道如何计算 - 我假设你的计算是正确的。

    【讨论】:

    • 这段代码一直有问题。当在此语句中时,它会引发错误 double AnnualSales = input.nextDouble();它一直说该方法应该是静态的。
    • 您是否按原样复制并粘贴代码?如果是这样,应该没有任何问题。无论如何,我猜你把input 放在main 方法之外。如果这样做,您需要将input 声明为static,即private static Scanner input
    • 我没有进行完整的复制和粘贴,但我确实将这两个部分都包含在了销售包中,并在 Person 类中取出了导入工具。
    • 好吧,如果没有看到您的代码,很难说出问题所在。你已经解决了这个问题吗?将两个类放在同一个包中,以及将两个类放在一个文件中应该可以正常工作。
    • 我更新了代码。正如它所说,我还在为两者添加带有输入的数组或数组列表。我们需要让多个人像以前一样询问姓名、薪水信息以及所得薪水之间的比较。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    • 2018-09-21
    • 1970-01-01
    • 2011-05-26
    相关资源
    最近更新 更多