嘿伙计们我正在为我的Comp Sci 1课程做实验工作,我们正在使用Bluej来学习java。当前的分配是让我们创建一个paystub,其中一些信息是通过键盘输入的,其余的是通过赋值给我们的,我遇到的问题是,虽然代码编译并且没有显示任何当我去测试它时,程序没有运行任何数学运算并将字段留空。我已经尝试了几个小时来解决这个问题,但我完全失败了。以下是我目前所拥有的内容,我非常感谢您对此事的任何建议,非常感谢。
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: $15.50 Reg Pay: $0.00
Overtime Hours: 15 OT Rate: $0.00 OT Pay: $0.00
Gross Pay: $0.00
SS Withholding: $0.00
Federal Tax: $0.00
Net Pay: $0.00
________________________________________________________________________________
答案 0 :(得分:1)
当您运行calculate()
时,您将引用作用于该方法的变量(例如double regularPay = ...
)。确保您正在修改您在班级顶部设置的私有成员变量:
public void calculate() {
this.regularPay = ...
//etc.
}
调用double regularPay = ...
将为该方法的范围创建一个名为regularPay
的新局部变量,一旦该方法终止,对该变量的引用就会丢失。私有成员变量不会被更改,因为它们完全是完全不同的变量。要更改这些内容,您可以使用this.myVariableName
引用它们。
答案 1 :(得分:1)
声明变量后,不再需要声明它。换句话说,在声明int value;
之类的变量之后,现在只需键入value
即可使用此变量。请记住像int value = 0;
一样初始化它。例如,您在顶部全局声明regularPay
,在calculate()
本地声明。
<强>范围强>
范围是可以看到变量的地方。如果在方法之外声明变量,那么您已将整个对象的可见性赋予该变量。如果在方法中声明变量,那么该方法是唯一可以使用该变量的地方。例外情况是返回值。然后可以检索该值,但退出方法后对象本身会丢失。
此关键字
如果在对象中全局声明变量并在方法中使用相同的变量名,则必须指定要引用的变量。然后使用关键字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;
}
}