加班计算器

时间:2016-03-07 20:55:51

标签: java

我一直在尝试用Java创建工资单计算器,但是我一直在考虑计算双薪的问题。这是我的代码:

if ( hoursWorked <= 36 ) {
      totalPay = (hourlyRate * hoursWorked);
} else if ( hoursWorked >= 37 && hoursWorked <= 40) {
      totalPay = (hourlyRate * 36) + (hoursWorked - 36) * (hourlyRate * 1.5);     
} else  //(hoursWorked > 41 ) {
      totalPay = (hourlyRate * 36) + (hoursWorked - 36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2); 
}

加班有效,但计算双倍工资导致支付不足。

3 个答案:

答案 0 :(得分:2)

最后一个其他段错误。(hoursWorked&gt; 41)应该是:

else { totalPay = (hourlyRate * 36) + 
                  (41 - 36) * (hourlyRate * 1.5) + 
                  (hoursWorked - 41) * (hourlyRate * 2); 
}

答案 1 :(得分:0)

if ( hoursWorked <= 36 ) {
      totalPay = (hourlyRate * hoursWorked);
} else if ( hoursWorked >= 37 && hoursWorked <= 40) {
      totalPay = (hourlyRate * 36) + (hoursWorked - 36) * (hourlyRate * 1.5);     
} else  //(hoursWorked > 41 ) {
      totalPay = (hourlyRate * 36) + ((hoursWorked - 36) * (hourlyRate * 1.5))
      int doublePay=(hoursWorked - 41) * (hourlyRate * 2); 
      totalPay=totalPay+doublePay;

}

答案 2 :(得分:0)

试试:

int tier1 = 0;
int tier2 = 0;
int tier3 = 0;
if (hoursWorked <= 36)
{
    tier1 = hoursWorked;
}else if (hoursWorked <= 40)
{
    tier1 = 36;
    tier2 = hoursWorked - tier1;
}else{
    tier1 = 36;
    tier2 = 4;
    tier3 = hoursWorked - tier2 - tier1;
}
totalPay = tier1* hourlyRate + tier2 * hourlyRate * 1.5 + tier3 * hourlyRate * 2;