0

我只是真的在学习 Java,正在阅读 Deitel 和 Deitel 的“Java 如何编程”一书中的教程,所以请原谅我犯的任何基本错误。我知道我的方法可能不是最好的,但我希望改进这一点。

我的问题是我相信我错误地构建了我的程序。执行程序时会输出 IF 和 ELSE 选项。

如果有人能告诉我为什么这两个选项都在执行,将不胜感激

困境

package controlStatments;
import java.util.Scanner;

public class Review_4_20 
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        //Declare Variables
        double employeeOneRate;
        double employeeOneHours;
        double employeeTwoRate;
        double employeeTwoHours;
        double employeeThreeRate;
        double employeeThreeHours;
        int calculator;
        double employeeOneTotalPay;
        double employeeOneNormalPay;
        double employeeOneTotalPayOverTime;
        double overTimeRate;

        //Initiate Variables
        employeeOneRate = 0;
        employeeOneHours = 0;
        employeeTwoRate = 0;
        employeeTwoHours = 0;
        employeeThreeRate = 0;
        employeeThreeHours = 0;
        calculator = 0;
        employeeOneTotalPay = 0;
        employeeOneTotalPayOverTime = 0;
        overTimeRate = 1.5; 
        employeeOneNormalPay = 0;

        //Create While Loop
        while (calculator != -1)
        {   
            //Prompt user to input details
            System.out.print("\n\nPlease input the first employees rate");
            employeeOneRate =input.nextDouble();

            System.out.print("Please input the first employees Hours");
            employeeOneHours =input.nextDouble();

            if (employeeOneHours <= 40)
            {
                employeeOneTotalPay = employeeOneHours * employeeOneRate;
                System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
            }
            else 
                employeeOneNormalPay = employeeOneRate * 40;
            employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
            System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);
        }
    }
}
4

3 回答 3

6

您忘记了 else 语句中的括号。

我想应该是这样的:

    if (employeeOneHours <= 40)
    {
        employeeOneTotalPay = employeeOneHours * employeeOneRate;
        System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
    }
    else {
        employeeOneNormalPay = employeeOneRate * 40;
        employeeOneTotalPayOverTime = (employeeOneHours - 40) * 
                (employeeOneRate * overTimeRate) + employeeOneNormalPay;                                
        System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);
    }

如果您不设置括号,则只有第一行包含在 IF/ELSE 语句中。

于 2012-04-21T16:29:27.923 回答
1

括号中的语句,例如if, else if,和循环仅在后面没有括号else时才执行紧随其后的行。

所以,你的声明:

if (employeeOneHours <= 40)
{
    employeeOneTotalPay = employeeOneHours * employeeOneRate;
    System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
}
else 
    employeeOneNormalPay = employeeOneRate * 40;
    employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
    System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);

总是成倍增加employeeOneNormalPay

将其更改为:

if (employeeOneHours <= 40) {
    employeeOneTotalPay = employeeOneHours * employeeOneRate;
    System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
} else {
    employeeOneNormalPay = employeeOneRate * 40;
    employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
    System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);
}
于 2012-04-21T16:31:08.170 回答
1

改变

if (employeeOneHours <= 40)
        {
            employeeOneTotalPay = employeeOneHours * employeeOneRate;
            System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
        }
        else 
            employeeOneNormalPay = employeeOneRate * 40;
        employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
        System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);

和:

if (employeeOneHours <= 40)
        {
            employeeOneTotalPay = employeeOneHours * employeeOneRate;
            System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
        }
        else 
       {
            employeeOneNormalPay = employeeOneRate * 40;
        employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
        System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);
}
于 2012-04-21T16:34:22.727 回答