gpt4 book ai didi

java - 我的 java while 循环没有返回每周工资

转载 作者:行者123 更新时间:2023-12-02 00:28:54 25 4
gpt4 key购买 nike

好的,这是第二部分。我重组了代码,但现在我无法找到一种方法来返回 empWeeklyPay 而不将其初始化为零。如果我将其初始化为零,那么我的工资将永远为零?有人能想到我能做些什么吗?

public double getWeeklyPay()//Call the getWeeklyHours method of the Emp's timecard to      get the total
//hours worked and then multiply the returned value by the emp's hourly rate and return the value.
{
TimeCard empTimeCard = timeCard;
double empWeeklyPay;
double totalOtHours;
double totalRegHours;
int i = 0;

while(i <= empTimeCard.NUMDAYS && empTimeCard.getHoursByDay(i) > 8)
{
double empHourlyRate = getHourlyRate();
double otHours = 0;
double regHours = 0;
double sumOtHours = 0;
int sumRegHours = 0;

//grabbing the overtime and reghours and storing them
otHours = empTimeCard.getHoursByDay(i) % 8;
regHours = empTimeCard.getHoursByDay(i) - otHours;
sumOtHours += otHours;
sumRegHours += regHours;
double tmpBasePay = (sumRegHours * empHourlyRate) + (sumOtHours * empHourlyRate * 1.5);

if(Integer.toString(getEmployeeId()).charAt(0) == '0' || Integer.toString(getEmployeeId()).charAt(0) == '2'
|| Integer.toString(getEmployeeId()).charAt(0) == '9')
//Java reads in 1010 so need to convert to a string to do a count on the value.
{
tmpBasePay += (tmpBasePay * .10);
i++;
}
else if(Integer.toString(getEmployeeId()).charAt(0) == '3')
{
tmpBasePay -= (tmpBasePay *.10);
i++;
}
else if(Integer.toString(getEmployeeId()).charAt(0) == '8')
{
tmpBasePay += (tmpBasePay * .20);
i++;
}
totalRegHours = regHours;
totalOtHours = sumOtHours;
if(totalRegHours > 34)
{
tmpBasePay -= (tmpBasePay * .06);
//empWeeklyPay = tmpBasePay;
empWeeklyPay = tmpBasePay;
}
else
{
empWeeklyPay = tmpBasePay;
}

}

return empWeeklyPay;// <---need to return back this value.   

最佳答案

您的第一个 while 循环 while 只执行一次迭代,因为它总是遇到 return 关键字。 return 将使您摆脱 while 循环您的 getWeekPay() 方法。

您只需在方法末尾返回您的 empWeeklyPay 变量,因此这里只有最后一个返回有用。方法的结尾应如下所示:

if(RegHours > 34) {
empBasePay -= (empBasePay * .06);
empWeeklyPay = empBasePay;
} else {
empWeeklyPay = empBasePay;
}
return empWeeklyPay;

编辑

为什么你总是得到 0:

double empBasePay = (RegHours * empHourlyRate) + (otHours * empHourlyRate * 1.5);

由于 RegHoursotHours 等于 0,您不妨这样写:

double empBasePay = 0;

由于该方法的其余部分基于 empBasePay 的基值(0),因此您的方法将始终返回 0。

关于java - 我的 java while 循环没有返回每周工资,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9523036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com