我很难将hours
和hourlyWage
参数传递给Paycheck
类中的构造函数。问题如下:
symbol: variable hours location : class Paycheck
对于公共班级Paycheck中的每小时工资或小时工资,它都会重复。
代码如下
import java.util.Scanner;
public class PayDayCalculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Hourly wage: ");
double hourlyWage = in.nextDouble();
System.out.println("Hours worked: ");
double hours = in.nextDouble();
Paycheck paycheck = new Paycheck(hourlyWage, hours);
System.out.println("Pay: " + paycheck.getPay());
}
}
public class Paycheck {
private double pay = 0;
private double overtime = 0;
private double overtimePay = 0;
/*double hours;
double hourlyWage; */
Paycheck(double hourlyWage, double hours) {
setPay(0);
}
public void setPay(double newPay) {
if (hours > 40) {
overtime = hours % 40;
hours = hours - overtime;
}
overtimePay = hourlyWage * 1.5;
pay = (hours * pay) + (overtime * overtimePay);
}
public double getPay() {
return pay;
}
}
答案 0 :(得分:1)
您已注释掉成员变量hours
:
/*double hours;
double hourlyWage; */
但仍尝试引用它,例如:
if (hours > 40) {
overtime = hours%40;
hours = hours - overtime;
}
如果您需要此变量 - 请取消注释。
答案 1 :(得分:0)
hours
和hourlyWage
未定义!
取消评论此部分 -
/*double hours;
double hourlyWage; */
答案 2 :(得分:0)
您的setPay
方法指的是hours
和hourlyWage
,它们是传递给构造函数的参数,使它们只对构造函数本地化。它们不适用于班级中的所有方法。如果您希望所有方法都可以访问它们,则需要在类级别取消注释。
double hours;
double hourlyWage;
Paycheck(double hourlyWage, double hours) {
this.hourlyWage = hourlyWage;
this.hours = hours;
setPay(0);
}