需要帮助计算两种不同薪级的工资!错误是净总数不正确。感谢所有评论,他们非常有帮助,现在它的工作原理:)我希望我的代码的这一部分可以帮助其他初学者学习使用循环和if语句:
public static void main(String[]args)
{
Scanner input = new Scanner (System.in);
DecimalFormat Currency = new DecimalFormat("###,###,##0.00");
double normHrs = 0, overHrs = 0, bonusHrs = 0, actualHrs = 0, normPay = 0, overPay =0, bonusPay = 0, grossWage = 0,vat23 = 0, netWage =0;
String empName, nextEMP = "y";
System.out.println ("************************");
System.out.println ("HUMAN RESOURCES: PAYROLL"); //
System.out.println ("************************");
while (nextEMP.equalsIgnoreCase("y"))
{
System.out.print("Please enter an employee's first name: ");
empName = input.next();
System.out.print("Please enter their total hours worked: ");
actualHrs = input.nextDouble();
if (actualHrs <=36)
{
normHrs=actualHrs;
}
else
{
normHrs = 36;
bonusHrs = actualHrs - 50;
overHrs = actualHrs -36;
}
if ("bob".equalsIgnoreCase(empName))
{
bonusPay = bonusHrs > 0 ? bonusHrs : 0;
normPay = normHrs * 8; // £8 x 36 hours
overPay = overHrs * 12; // £12 x 36+ hours
}
else
{
bonusPay = bonusHrs >0 ? 20 : 0;
normPay = normHrs * 5.75;
overPay = overHrs * 9.25;
}
}
答案 0 :(得分:2)
在if块中,在分配 normPay 时出错:
if ("bob".equalsIgnoreCase(empName)) {
bonusPay = bonusHrs > 50 ? bonusHrs : 0;
normPay = actualHrs * 8; // £8 x 36 hours
overPay = overHrs * 12; // £12 x 36+ hours
}
一定是
normPay = normHrs * 8;
答案 1 :(得分:1)
我认为错误在评估Bob是否应该获得奖金的部分。
由于您已将bonusHrs
计算为actualHrs - 50
,因此:
bonusPay = bonusHrs > 50 ? bonusHrs : 0;
仅在actualHrs
为100+时才会给予Bob奖金。应该是:
bonusPay = bonusHrs > 0 ? bonusHrs : 0;
另外,这个:
normPay = actualHrs * 8; // £8 x 36 hours
可能是错的,应该是:
normPay = normHrs * 8; // £8 x 36 hours