我只是学习如何使用Java编写代码,而且我遇到了错误。
该行:“工资+ =(超时时间*基本支付* 1.5);”如果给我一些问题。确切的错误是:
overtimeHours cannot be resolved to a variable
但是,我用这一行创建了上面的变量:
int overtimeHours = hours - 40;
那么,我在这里做错了什么?
public class base_pay {
// create two methods in the base_pay class
// first method is pay, the second is main() to run the program
public static void pay(double basePay, int hours) {
if (basePay < 8.0) {
System.out.println("You must be paid at least $8.00/hr");
} else if (hours > 60) {
System.out.println("You cannot work more than 60 hr pr week");
} else {
// define what overtime is here
int overtime = 0;
if (hours > 40) {
int overtimeHours = hours - 40;
hours = 40; // Because anything over 40 is overtime .. if overtime was 50 hours than use 50
}
double wage = basePay*hours;
wage += (overtimeHours * basePay * 1.5);
System.out.println("Your total pay is: " + wage);
}
}
public static void main(String[] args) {
// going to run pay above, and see what happens
pay(8.5, 45);
}
}
通过自己的问题回答。我将变量定义了两次。
答案 0 :(得分:1)
我一发布就知道了。我用“int”两次定义变量...所以我不得不删除它。