如何获取Java来计算字段?

How to get Java to calculate fields?

大家好,我目前正在为我的 Comp Sci 1 class 做一个实验室作业,其中我们使用 Bluej 来学习 java。当前的任务是让我们创建一个工资单,其中一些信息是通过键盘输入的,其余信息是通过任务提供给我们的,我 运行 遇到的问题是,虽然代码编译但没有当我去测试它时,请告诉我任何错误该程序没有 运行 任何数学运算并将字段留空。我已经尝试了几个小时来解决这个问题,但我完全不知所措。下面是我目前所拥有的,我非常感谢任何关于此事的建议,非常感谢。

    import java.util.Scanner;
/**
 * Activity1PayStub class is part of Lab 3 and
 * creates a simple pay stub.
 *
 * @author Nicholas Thomas
 * @version 2/6/2018
 */
public class Activity2PayStub
{
    public static final double OVERTIME_FACTOR = 1.5; 
    public static final double SOCIAL_SECURITY_WITHHOLDING = .10;
    public static final double FEDERAL_TAX = .20;
    private String employeeName;
    private String employeeSocialSecurityNumber;
    private int regularHoursWorked;
    private double hourlyPayRate;
    private int overtimeHoursWorked;
    private double regularPay = (hourlyPayRate + regularHoursWorked);
    private double overtimeRate = (OVERTIME_FACTOR * hourlyPayRate);
    private double overtimePay = (overtimeHoursWorked * overtimeRate);
    private double grossPay = (regularPay + overtimePay);
    private double socialSecurityWithholding = (grossPay 
            * SOCIAL_SECURITY_WITHHOLDING);
    private double federalTax = (grossPay - socialSecurityWithholding) 
        * FEDERAL_TAX;
    private double netPay = grossPay - (federalTax + socialSecurityWithholding);
    /**
     * It all starts with the main method.
     *
     * @param args command-line arguments (not used)
     */

    public static void main(String[] args)
    {     
        Scanner keyboard = new Scanner(System.in);
        //Create an Activity2Paystub object
        //a2ps is an Activity2PayStub object
        Activity2PayStub a2ps = new Activity2PayStub();
        //call the methods inside of an Activity2PayStub object
        a2ps.getInput(keyboard);
        a2ps.calculate();
        a2ps.printPayStub();
    }

    /** This is to ensure the keyboard input can be received.
     * Method getInput
     *
     * @param keyboard A parameter
     */
    public void getInput(Scanner keyboard)         
    {
        System.out.print("Employee Name: ");
        employeeName = keyboard.nextLine();
        System.out.print("Social Security Number: ");
        employeeSocialSecurityNumber = keyboard.nextLine();
        System.out.print("Regular Hours Worked: ");       
        regularHoursWorked = keyboard.nextInt();
        System.out.print("Overtime Hours Worked: ");       
        overtimeHoursWorked = keyboard.nextInt();
        System.out.print("Hourly Pay Rate: ");
        hourlyPayRate = keyboard.nextDouble();
    }

    /** Method to do all the math calculations.
     * Method calculate
     *
     */
    public void calculate()
    {
        double regularPay = (hourlyPayRate * regularHoursWorked);
        double overtimeRate = (OVERTIME_FACTOR * hourlyPayRate);
        double overtimePay = (overtimeHoursWorked * overtimeRate);
        double grossPay = (regularPay + overtimePay);
        double socialSecurityWithholding = (grossPay 
                * SOCIAL_SECURITY_WITHHOLDING);
        double federalTax = (grossPay - socialSecurityWithholding) 
            * FEDERAL_TAX;
        double netPay = grossPay - (federalTax + socialSecurityWithholding);
    }

    /** Lets us print and format our stubs.
     * Method printPayStub
     *
     */
    public void printPayStub()
    {
        String format1 = "Name: %-37s SSN: %-11s\n";
        String format2 = "Regular Hours: %-8d Reg Rate: $%-8.2f " 
            + "Reg Pay: $%-8.2f\n";
        String format3 = "Overtime Hours: %-8dOT Rate: $%-8.2f " 
            + " OT Pay: $%-8.2f\n";
        String format4 = "Gross Pay: $%-8.2f\n";
        String format5 = "SS Withholding: $%-8.2f\n";
        String format6 = "Federal Tax: $%-8.2f\n";
        String format7 = "Net Pay: $%-8.2f\n";
        System.out.println("________________________________________"
            + "________________________________________");
        System.out.printf(format1, employeeName, employeeSocialSecurityNumber);
        System.out.printf(format2, regularHoursWorked,
            hourlyPayRate, regularPay);
        System.out.printf(format3, overtimeHoursWorked,  
            overtimeRate, overtimePay);
        System.out.printf(format4, grossPay);
        System.out.printf(format5, socialSecurityWithholding);
        System.out.printf(format6, federalTax);
        System.out.printf(format7, netPay);
        System.out.println("________________________________________"
            + "________________________________________");
    }
}

我目前得到的结果

    ________________________________________________________________________________
Name: Tim Buctoo                            SSN: 111-11-1112
Regular Hours: 40       Reg Rate: .50    Reg Pay: [=12=].00    
Overtime Hours: 15      OT Rate: [=12=].00      OT Pay: [=12=].00    
Gross Pay: [=12=].00    
SS Withholding: [=12=].00    
Federal Tax: [=12=].00    
Net Pay: [=12=].00    
________________________________________________________________________________

当您 运行 calculate() 时,您正在引用方法范围内的变量(例如 double regularPay = ...)。确保您正在修改您在 class:

顶部设置的私有成员变量
public void calculate() {
    this.regularPay = ...
    //etc.
}

调用 double regularPay = ... 将为该方法的范围创建一个名为 regularPay 的新局部变量,一旦该方法终止,对该变量的引用就会丢失。私有成员变量不会改变,因为它们完全是完全不同的变量。要更改这些,您可以使用 this.myVariableName.

引用它们

一旦你声明了一个变量,它就不再需要声明了。换句话说,在你声明了一个变量如 int value; 之后,你现在只需输入 value 就可以使用这个变量。记得像这样初始化它int value = 0;。例如,您在 calculate().

的顶部和局部声明了 regularPay

范围

Scope是可以看到变量的地方。如果您在方法之外声明变量,那么您已经为该变量提供了整个对象可见性。如果您在方法内部声明变量,那么该方法是唯一可以使用该变量的地方。例外情况是返回值。然后可以检索值,但对象本身在退出方法后丢失。

这个关键词

如果您在一个对象中全局声明一个变量并在一个方法中使用相同的变量名,那么您必须指定您指的是哪个变量。然后使用关键字 this 指定正在使用的对象内的变量。例如

public class Values{
    //Global in respect to object
    private int value = 0;

    public void setValue(int value) {
        //The variable "value" passed in is local
        this.value = value;
    }
}