不知道问题出在哪里?

时间:2016-02-05 00:44:00

标签: java if-statement

我对此程序的输出有疑问。我收到一条关于从布尔值转换为int的错误消息,即使我没有看到它。有没有办法可以在switch语句中使用双精度或转换为if语句?

Scanner input = new Scanner(System.in);

    int hourlyWage = 0;
    int hour = 0;
    int grossPay = 0;

    System.out.print("Please enter the employee name: ");
    String name = input.nextLine();

    System.out.print("Please enter an hourly wage:$ ");
    hourlyWage = input.nextInt();

    System.out.println("Please enter the number of hours worked: ");
    hour = input.nextInt();

    final double OVERTIME = 1.5;
    final double REG_HOUR = 40;

    switch(hourlyWage){
    case (hourlyWage >= 7.25):
        if(hour <= 40){
            grossPay = hour * hourlyWage;
        }
        else if(hour > 40){
            grossPay = (int) ((int) (hour * hourlyWage) + ((hour - REG_HOUR) * (hourlyWage * OVERTIME)));
        }
        break;
    default:
        System.out.println("The wage entered is too low. Please try again.");
        System.exit(1);
        break;
    }

    System.out.println("");
    System.out.println("ABC Manufacturing Company");
    System.out.println("Name  Hours  Pay");
    System.out.printf("%s  %.2f  $%.2f", name, hour, grossPay);
}

}

1 个答案:

答案 0 :(得分:1)

问题出在这一行

case (hourlyWage >= 7.25):

hourlyWage&gt; = 7.25是一个计算结果为true或false(布尔值)的表达式。

我真的不明白为什么你需要一个开关,你可以这样做:

if(hourlyWage >= 7.25){
    if(hour <= 40){
        grossPay = hour * hourlyWage;
    }
    else {
        grossPay = (int) ((int) (hour * hourlyWage) + ((hour - REG_HOUR) * (hourlyWage * OVERTIME)));
    }
}
else {
    System.out.println("The wage entered is too low. Please try again.");
    System.exit(1);
}