2012-03-01 3 views
0

オーケー週給を戻っていない間。私はこれでコードを再構築しましたが、empWeeklyPayを0に初期化せずに返却する方法を見つけることができません。それをゼロに初期化すれば、私の給料は常にゼロになりますか?誰も私ができることを考えることができますか?私のJavaのループが一部2ので、この上

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キーワードに遭遇したとしてのみ一回の反復を行いながら

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

これは 'return'ステートメントの*ロット*です。アイデアが実際にループするのであれば、あなたは 'i'をインクリメントした後に' return'するので、ループを止めてしまいます。 –

+1

書式を修正してください。コードはそのまま読めるわけではありません。 –

+0

大丈夫ああ私は、リターンは私が私のことを警告するためにそのコードnow..thanksを編集した – SaintClaire33

答えて

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ループ。 returnwhileループあなたgetWeekPay()方法をあなたを取得します。

あなたは自分のメソッドの最後に、あなたのempWeeklyPay変数を返す必要があるので、最後のリターンはここに便利です。あなたは常に0を取得しているのはなぜ

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

EDIT

:あなたの方法の終わりは次のようになります

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

otHours等しい0、あなたにも書くかもしれません:

double empBasePay = 0; 

A ndメソッドの残りの部分は、ベース値がempBasePay(これは0)に基づいています。メソッドは常に0を返します。

+0

[OK]を私は他にあれば、あなたは 'if'、' else'にし、 'while'すぎでものをコメントアウトする必要がある一つの第一.. – SaintClaire33

+0

場合はそれぞれにreturn文をコメントアウトlook..illてみましょう。 – talnicolas

+0

それはあなたにタルニコラありがとうでしたか?それはまだゼロを返すようです...うーん。私が今持っている唯一のリターンステートメントは最後です。これは、whileステートメントのいずれも真でなく、タイムカード上の7日間のすべての時間が満たされていない場合、時間が34時間を超えるかどうかを確認するチェックを行います。これらは現在残っている唯一のリターンステートメントです。私の値はおそらく変数に適切に保存されていないでしょうか? – SaintClaire33

関連する問題