由于某种原因,我得到的数学值与应该获得的值不同。我在这里做错了什么?
码:
overtimeHours = hoursWorked - 40;
regularPay = hourlyWage * hoursWorked - overtimeHours;
overtimePay = 1.5 * hourlyWage * overtimeHours;
totalPay = hourlyWage * hoursWorked + overtimePay;
输出:
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
最佳答案
运算顺序,就像您在基础数学中学到的一样。你在算
(hourlyWage * hoursWorked) - overtimeHours
相反,您想要
hourlyWage * (hoursWorked - overtimeHours)
另外,您不应该在
total
中重新计算。相反,只需添加定期和加班。最后,正如@CodeMonkey所指出的,您无条件地假设此人正在加班。这是我的建议:
if(hoursWorked > 40) {
regularHours = 40;
overtimeHours = regularHours - 40;
} else {
regularHours = hoursWorked;
overtimeHours = 0;
}