如何让Java计算字段?
嘿,伙计们,我目前正在为我的Comp Sci 1课程做一个实验作业,在这个课程中,我们使用Bluej学习java。当前的任务是让我们创建一个工资存根,其中一些信息通过键盘输入,其余信息通过任务提供给我们,我遇到的问题是,虽然代码经过编译,在我去测试时没有显示任何错误,但程序没有运行任何数学运算,字段为空。我花了几个小时想弄明白,但我完全不知所措。下面是我目前拥有的,我非常感谢您对此事的任何建议,非常感谢如何让Java计算字段?,java,bluej,Java,Bluej,嘿,伙计们,我目前正在为我的Comp Sci 1课程做一个实验作业,在这个课程中,我们使用Bluej学习java。当前的任务是让我们创建一个工资存根,其中一些信息通过键盘输入,其余信息通过任务提供给我们,我遇到的问题是,虽然代码经过编译,在我去测试时没有显示任何错误,但程序没有运行任何数学运算,字段为空。我花了几个小时想弄明白,但我完全不知所措。下面是我目前拥有的,我非常感谢您对此事的任何建议,非常感谢 import java.util.Scanner; /** * Activity1
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
________________________________________________________________________________
当您运行
calculate()
时,您正在引用该方法范围内的变量(例如double regularPay=…
)。确保正在修改在类顶部设置的私有成员变量:
public void calculate() {
this.regularPay = ...
//etc.
}
调用
double regularPay=…
将为该方法的作用域创建一个名为regularPay
的新局部变量,一旦该方法终止,对该变量的引用将丢失。私有成员变量不会更改,因为它们是完全不同的变量。要更改这些变量,可以在运行calculate()
时使用this.myVariableName引用这些变量,因为您引用的变量的作用域是该方法(例如double regularPay=…
)。确保正在修改在类顶部设置的私有成员变量:
public void calculate() {
this.regularPay = ...
//etc.
}
调用double regularPay=…
将为该方法的作用域创建一个名为regularPay
的新局部变量,一旦该方法终止,对该变量的引用将丢失。私有成员变量不会更改,因为它们是完全不同的变量。要更改这些变量,您可以使用this.myVariableName
引用它们,一旦您声明了一个变量,就不必再声明它了。换句话说,在声明变量后,例如int-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;
}
}
一旦您声明了一个变量,就不再需要声明它了。换句话说,在声明变量后,例如int-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;
}
}
你知道如何使用调试模式吗?我知道的唯一一件事就是终止当前正在运行的程序,不能让它做很多其他的事情。你知道如何使用调试模式吗?我知道的唯一一件事就是终止当前正在运行的程序,不能让它做很多其他的事情。非常感谢你,我不知道如果不是那样的话,我会花多长时间来解决这个问题。非常感谢你,我不知道如果不是那样的话,我会花多长时间来解决这个问题。是的,我已经为此工作了几个小时,所以我一直在以不同的方式声明变量,只是忘了删除它,我对此一无所知。会有用的。我建议你花点时间阅读关键词“this”。它将有助于澄清什么时候是必要的,什么时候不是。还记得为有这个问题的未来用户标记一个正确的答案。是的,我已经在这方面工作了几个小时,所以我一直在以不同的方式声明这两个地方的变量,只是忘记删除它,我不知道这一点。会有用的。我建议你花点时间阅读关键词“this”。它将有助于澄清什么时候是必要的,什么时候不是。另外,请记住为将来有此问题的用户标记正确答案。