简体   繁体   English

数学值无法正确计算

[英]Math Values not Calculating Properly

For some reason, I am getting different mathematical values than I should be. 由于某种原因,我得到的数学值与应有的值有所不同。 What am I doing wrong here? 我在这里做错了什么?

Code: 码:

overtimeHours = hoursWorked - 40;

regularPay = hourlyWage * hoursWorked - overtimeHours;
overtimePay = 1.5 * hourlyWage * overtimeHours;
totalPay = hourlyWage * hoursWorked + overtimePay;

Output: 输出:

Please enter employee's name: John
Please enter The Dragon's hourly wage: 22.50
Please enter how many hours The Dragon worked: 48.5

John's Wages
Regular Pay: $    1082.75
Overtime Pay: $    286.88
Total Pay: $    1378.13

Order of operations, just like you learned in basic math. 运算顺序,就像您从基础数学中学到的一样。 You're calculating 你在计算

(hourlyWage * hoursWorked) - overtimeHours

Instead, you want 相反,您想要

hourlyWage * (hoursWorked - overtimeHours)

Additionally, you shouldn't recalculate in your total ; 另外,您不应该重新计算total instead, just add regular and overtime. 相反,只需添加常规时间和加班时间即可。

Finally, as @CodeMonkey notes, you're unconditionally assuming that the person is working overtime. 最后,正如@CodeMonkey所指出的,您无条件地假设该人正在加班。 Here's what I suggest instead: 这是我的建议:

if(hoursWorked > 40) {
    regularHours = 40;
    overtimeHours = regularHours - 40;
} else {
    regularHours = hoursWorked;
    overtimeHours = 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM