2012-03-01 66 views
0

好吧,所以第二部分就這一點。我重構了代碼,但現在我無法找到一種方法來返回empWeeklyPay,而無需將其初始化爲零。如果我將它初始化爲零,那麼我的工資總是零?任何人都可以想到我能做的任何事情嗎?我的java while循環沒有返回每週薪酬

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. 
+1

這是* return *語句。如果這個想法實際上是循環的,那麼你沒有這樣做,因爲你在遞增'i'之後'返回',從而停止循環。 – 2012-03-01 19:58:08

+1

請修復您的格式。代碼不可能按原樣讀取。 – 2012-03-01 19:58:31

+0

啊好吧我認爲回報是搞砸了..ok讓我改變.. – SaintClaire33 2012-03-01 20:00:37

回答

1

以下是我的問題的答案。我能弄明白。請參閱下面的代碼以防其他人在近期或遠期將面臨同樣的問題:

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; 
} 
2

你的第一個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; 

A因爲該方法的其餘部分基於empBasePay的基數值,即0,所以您的方法將始終返回0.

+0

好吧,讓我看一下,如果在每個if和else if中都註釋掉return語句。 – SaintClaire33 2012-03-01 20:04:04

+0

您還需要註釋'if','else'和'while'中的那些。 – talnicolas 2012-03-01 20:04:57

+0

好的,謝謝你talnicolas。它似乎仍然回到零...嗯。我現在唯一的回報聲明就是結尾。如果沒有任何一個while語句是真實的,並且計時卡上7天的所有小時數都被填充,那麼它將執行檢查以驗證小時是否大於34小時。那些是現在剩下的唯一回報聲明。那麼我的價值觀是不是可以正確保存在變量中呢? – SaintClaire33 2012-03-01 20:15:24