我正在尝试替换payRate * hoursWorked
为单个变量grossPay
,但是当我这样做时,它会为任何计算返回 0.0 的值。double grossPay
被故意注释掉,所以你可以看到我想要做什么。
import java.text.*;
import java.util.Scanner;
public class elseIfPayroll{
public static void main(String[] args){
double hoursWorked =0.00;
double payRate =0.00;
int dependents =0;
Scanner keyboard = new Scanner(System.in);
NumberFormat f = new DecimalFormat("#0.00");
System.out.print("Enter pay rate: ");
payRate = keyboard.nextInt();
System.out.print("Enter hours worked: ");
hoursWorked = keyboard.nextInt();
System.out.print("Enter amount of dependents: ");
dependents = keyboard.nextInt();
double grossPay= (payRate * hoursWorked);
//gross pay decision statements
if (hoursWorked<=40) {
System.out.println("Total $ Earned: " + payRate * hoursWorked);
}
else if (hoursWorked >= 40) {
System.out.println("Total $ Earned: " + payRate * hoursWorked +(payRate * 0.5));
}
else if (hoursWorked>=60){
System.out.println("Total $ Earned: " + payRate * hoursWorked + (payRate * 2));
}
//else if statements for dependents
if (dependents == 0){
f.format(payRate * hoursWorked);
}
else if (dependents == 1){
f.format(payRate * hoursWorked - (payRate * hoursWorked*0.04) );
}
else if (dependents == 2){
f.format(payRate * hoursWorked - (payRate * hoursWorked*0.0775));
}
else if (dependents == 3){
f.format(payRate * hoursWorked - (payRate * hoursWorked*0.1125) );
}
else if (dependents == 4){
f.format(payRate * hoursWorked - (payRate * hoursWorked*0.145) );
}
else if (dependents == 5){
f.format(payRate * hoursWorked - (payRate * hoursWorked*0.175) );
}
else if (dependents >= 6){
f.format(payRate * hoursWorked - (payRate * hoursWorked*0.2025) );
}
System.out.println("New gross Pay: " + f.format(payRate * hoursWorked - (payRate * hoursWorked*0.04)));
}
}