gpt4 book ai didi

Java, "Variable name"无法解析为变量

转载 作者:太空狗 更新时间:2023-10-29 22:39:43 24 4
gpt4 key购买 nike

我使用 Eclipse 和 Java,我得到这个错误:

"Variable name" cannot be resolved to a variable.

使用这个 Java 程序:

public class SalCal {
private int hoursWorked;
public SalCal(String name, int hours, double hoursRate) {
nameEmployee = name;
hoursWorked = hours;
ratePrHour = hoursRate;
}
public void setHoursWorked() {
hoursWorked = hours; //ERROR HERE, hours cannot be resolved to a type
}
public double calculateSalary() {
if (hoursWorked <= 40) {
totalSalary = ratePrHour * (double) hoursWorked;
}
if (hoursWorked > 40) {
salaryAfter40 = hoursWorked - 40;
totalSalary = (ratePrHour * 40)
+ (ratePrHour * 1.5 * salaryAfter40);
}
return totalSalary;
}
}

导致此错误消息的原因是什么?

最佳答案

如果您查看变量“hoursWorked”的范围,您会发现它是该类的成员(声明为 private int)

您遇到问题的两个变量作为参数传递给构造函数。

错误消息是因为“小时”超出了 setter 的范围。

关于Java, "Variable name"无法解析为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7588784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com