lundi 9 mars 2015

Error: cannot be resolved to a variable



Im just learning how to code in Java, and I'm running into an error.


The line: "wage += (overtimeHours * basePay * 1.5);" if giving me some problems. The exact error is:



overtimeHours cannot be resolved to a variable


However, I have created the variable above with this line:



int overtimeHours = hours - 40;


So, what am I doing wrong here?



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);
}

}




Answered by own question. I defined the variable twice.




Aucun commentaire:

Enregistrer un commentaire