1

我很难将hoursandhourlyWage参数传递给Paycheck类中的构造函数。问题如下:

symbol: variable hours
location : class 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;
    }
}
4

3 回答 3

1

您已注释掉成员变量hours

/*double hours;
double hourlyWage; */

但仍然尝试引用它,例如:

if (hours > 40) {
    overtime = hours%40;
    hours = hours - overtime;
}

如果您需要此变量 - 取消注释它。

于 2013-04-25T17:23:11.743 回答
0

hours并且hourlyWage没有定义!

取消注释这部分 -

/*double hours;
double hourlyWage; */
于 2013-04-25T17:24:16.480 回答
0

您的setPay方法是指hoursand hourlyWage,它们是传递给构造函数的参数,使它们仅对构造函数是本地的。它们不适用于类中的所有方法。如果您希望所有方法都可以访问它们,则需要在类级别取消注释。

double hours;
double hourlyWage;

Paycheck(double hourlyWage, double hours) {
    this.hourlyWage = hourlyWage;
    this.hours = hours;
    setPay(0);
}
于 2013-04-25T17:24:29.710 回答