在Java中错误地执行While循环中的“if”和“else”
我只是在真正学习Java,通过Deitel和Deitel的《Java如何编程》一书中的教程,所以请原谅我犯的任何基本错误。我知道我的方法可能不是最好的,但我希望改进这一点 我的问题是我认为我的程序构建错误。当我执行程序时,IF和ELSE选项都被输出 如果有人能告诉我为什么这两个选项都在执行,我将不胜感激 深水区在Java中错误地执行While循环中的“if”和“else”,java,if-statement,while-loop,Java,If Statement,While Loop,我只是在真正学习Java,通过Deitel和Deitel的《Java如何编程》一书中的教程,所以请原谅我犯的任何基本错误。我知道我的方法可能不是最好的,但我希望改进这一点 我的问题是我认为我的程序构建错误。当我执行程序时,IF和ELSE选项都被输出 如果有人能告诉我为什么这两个选项都在执行,我将不胜感激 深水区 package controlStatments; import java.util.Scanner; public class Review_4_20 { public s
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);
}
}
}
您忘记了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语句中只包含第一行。括号内的语句,如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);
改变
与:
Java允许内联初始化变量;i、 e.不需要声明然后初始化,只需在一行中声明并初始化:double overTimeRate=1.5;这将使您的示例更具可读性。感谢Arturs,这将有很大帮助。感谢大家的回复,下次请不要忘记这一点time@deepend总是使用大括号不是一个坏习惯,即使它们是可选的。有时我们会抵制,因为只有一个语句时没有括号看起来更优雅,但我从未听说过有人后悔使用括号,因为括号是可选的,但每个人都至少有一种情况,他们希望自己只使用括号。
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);
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);
}