变量 netPay on
System.out.println("\t Regular Pay for 40.0 hour(s): $" + netPay);
一直说它没有初始化。它唯一有效的时候是当我使用 if-else 语句时。
public static void main(String [] args){
int employeeNumber;
double hourlySalary, weeklyTime, netPay, overTime, overTimehours;
Scanner keyboard= new Scanner(System.in);
System.out.println("Welcome to \"TEMPLE HUMAN RESOURCES\"");
System.out.println();
System.out.print("\t Enter Employee Number: ");
employeeNumber=keyboard.nextInt();
System.out.print("\t Enter Hourly Salary: ");
hourlySalary=keyboard.nextDouble();
System.out.print("\t Enter Weekly Time: ");
weeklyTime=keyboard.nextDouble();
System.out.println();
overTimehours=weeklyTime-40;
overTime=(1.5*10)*hourlySalary;
if (weeklyTime>40) {
netPay=(weeklyTime*hourlySalary)+(overTime*overTimehours);
}else if (weeklyTime==40){
netPay=40*hourlySalary;
}else {
overTimehours=0;
overTime=0;
}
System.out.println("\t Employee #: " + employeeNumber);
System.out.println("\t Hourly Salary: $" + hourlySalary);
System.out.println("\t Weekly Time: " + weeklyTime + " hour(s)");
System.out.println("\t Regular Pay for 40.0 hour(s): $" + netPay);
System.out.println("\t Overtime Pay for " +overTimehours+ " hour(s): $" + overTime);
System.out.println("\t Net Pay: $" + (overTime+ netPay));
System.out.println();
System.out.println("Thank you for using \"Temple Human Resources\"");
}