好的,这是第二部分。我在此基础上重新编写了代码,但现在我想不出在不将empWeeklyPay初始化为0的情况下返回它的方法。如果我将它初始化为零,那么我的工资将始终为零?有谁能想到我能做些什么吗?
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. 发布于 2012-03-07 06:50:57
这是我问题的答案。我能够弄明白这一点。请查看下面的代码,以防其他人在不久或遥远的将来遇到同样的问题:
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.
double empWeeklyPay;
double sumOtHours = 0;
double sumRegHours = 0;
int i = 0;
while(i < getTimeCard().NUMDAYS) {
// grabbing the overtime and regHours and storing them
double otHours = 0;
double regHours = 0;
otHours += getTimeCard().getHoursByDay(i);
regHours += getTimeCard().getHoursByDay(i) - otHours;
sumOtHours += otHours;
sumRegHours += regHours;
i++;
}
empWeeklyPay = (sumRegHours * getHourlyRate()) + (sumOtHours * getHourlyRate() * 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.
double tmpBasePay = (sumRegHours * getHourlyRate()) + (sumOtHours * getHourlyRate() * 1.5);
tmpBasePay += (tmpBasePay * .10);
empWeeklyPay = tmpBasePay;
}
else if(Integer.toString(getEmployeeId()).charAt(0) == '3') {
double tmpBasePay = (sumRegHours * getHourlyRate()) + (sumOtHours * getHourlyRate() * 1.5);
tmpBasePay -= (tmpBasePay * .10);
empWeeklyPay = tmpBasePay;
}
else if(Integer.toString(getEmployeeId()).charAt(0) == '8') {
double tmpBasePay = (sumRegHours * getHourlyRate()) + (sumOtHours * getHourlyRate() * 1.5);
tmpBasePay += (tmpBasePay * .20);
empWeeklyPay = tmpBasePay;
}
if(sumRegHours > 34) {
empWeeklyPay -= (empWeeklyPay * .06 );
return empWeeklyPay;
}
else
return empWeeklyPay;
}发布于 2012-03-02 04:00:16
由于总是遇到return关键字,您的第一个while循环只执行一次迭代。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);当RegHours和otHours等于0时,您不妨这样写:
double empBasePay = 0;由于该方法的其余部分基于empBasePay的基值,即0,因此您的方法将始终返回0。
https://stackoverflow.com/questions/9523036
复制相似问题