在Java中的While循环中错误地执行“if”和“else”(Wrongly executing both “if” and “else” in While loop in Java)

编程入门 行业动态 更新时间:2024-10-27 22:23:59
在Java中的While循环中错误地执行“if”和“else”(Wrongly executing both “if” and “else” in While loop in Java)

我只是在学习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); } } }

I’m just really learning Java, going through tutorials in book "Java how to Program" by Deitel and Deitel, so please forgive any basic mistakes I’m making. I understand my methodology might not be the best but I hope to improve this.

My problem is I believe I have constructed my program wrongly. Both the IF and ELSE options are being outputted when I execute the program.

If anyone could tell me why both options are executing it would be very much appreciated

Deepend

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); } } }

最满意答案

更改

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); }

Change

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);

With:

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); }

更多推荐

本文发布于:2023-08-04 12:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416142.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Java   Wrongly   loop   executing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!