2013-08-26 66 views
1

我在獲取數組中整數的總和以及獲取整數乘積* 1.5的問題時出現問題。我的下面的代碼可能是完全關閉的,因爲我是Java新手,並且已經在這裏工作了好幾個小時。該計劃的目的是輸入每天工作5天的工作小時數。有了這個和工資率,你應該輸出平均工作小時數,總小時數和總工資。如果有工資,工資也應該包括加班費。任何幫助,將不勝感激。數組中的整數和整數乘以1.5的總和

String name; 
String id; 
int payRate; 
int[] hours = new int[5]; 
int avgHours; 
int totalPay; 
int totalHours = 0; 
int counter; 
int overTime = 0; 

//housekeeping 
System.out.print("Enter the Employee's name: "); 
inputString = input.readLine(); 
name = inputString; 

System.out.print("Enter the Employee's ID: "); 
inputString = input.readLine(); 
id = inputString; 

System.out.print("Enter the Employee's pay rate: "); 
inputString = input.readLine(); 
payRate = Integer.parseInt(inputString); 

//hoursPay 
counter = 0; 
for(hours[counter] = 0; counter < 5; counter++) 
{ 
    System.out.print("How many hours did the employee work? "); 
    inputString = input.readLine(); 
    hours[counter] = Integer.parseInt(inputString); 
}//endfor 
    for(totalHours = 0; counter < 5; hours[counter]++); 
    { 
     totalHours += hours[counter]; 
     if(totalHours > 40) 
     { 
      overTime = payRate + (payRate/2); 
     }//endif 
    }//endwhile 

//print 
if(counter == 5) 
{ 
    System.out.println(name + " " + id + " $" + payRate + "/hour"); 

    avgHours = totalHours/counter; 
    totalPay = totalHours * payRate + overTime; 
    System.out.println... 
    System.out.println... 
    System.out.println... 
+4

您不會在while循環內增加計數器。 – Keith

+0

Keith說,再加上'totalHours'上的'if' block測試可能應該在循環之外。 –

+0

爲了將來的參考,您希望獲得關於「問題」的更多技術信息。 –

回答

0

@ bp_1, 我又重新做所有的代碼,並粘貼下面它。有用。編碼時出現了一些基本錯誤。將您的代碼與我的代碼進行比較,您將看到不同之處。

String name; 
String id; 
int payRate; 
int[] hours = new int[5]; 
int avgHours; 
int totalPay; 
int totalHours = 0; 
int counter; 
int overTime = 0; 
Scanner input = new Scanner(System.in); 
//housekeeping 
System.out.print("Enter the Employee's name: "); 
String inputString = input.nextLine(); 
name = inputString; 

System.out.print("Enter the Employee's ID: "); 
inputString = input.nextLine(); 
id = inputString; 

System.out.print("Enter the Employee's pay rate: "); 
inputString = input.nextLine(); 
payRate = Integer.parseInt(inputString); 

//hoursPay 
counter = 0; 
for (hours[counter] = 0; counter < 5; counter++) { 
System.out.print("How many hours did the employee work? "); 
inputString = input.nextLine(); 
hours[counter] = Integer.parseInt(inputString); 
}//endfor 

counter = 0;// reset counter here 
for (totalHours = 0; counter < 5; counter++) { 
totalHours += hours[counter]; 
if (totalHours > 40) { 
overTime = payRate + (payRate/2); 
}//endif 
}//end of for loop 

if (counter == 5) { 
System.out.println(name + " " + id + " $" + payRate + "/hour"); 
avgHours = totalHours/counter; 
totalPay = totalHours * payRate + overTime; 
System.out.println("Average Hours: " + avgHours); 
System.out.println("Total pay: " + totalPay); 
System.out.println("Total Hours: " + totalHours); 
System.out.println("Overtime ($): " + overTime); 
}//end of if 
+0

你不必去所有的麻煩,但它是非常感激。我現在會比較兩者,以便從錯誤中學習。 –

+0

@ bp_1匆忙中,我忘了告訴你,我故意在程序中留下了一個邏輯錯誤。提示,它是** **加班**計算。試着解決它,你會學到一些更多的Java。 –

+0

今天早些時候我發現了這個錯誤。再次感謝你的幫助 –

0

代替

for(totalHours = 0; counter < 5; hours[counter]++); 

寫的

for(counter = 0; counter < 5; counter++) 
  1. 分號刪除。
  2. counter遞增,而不是hours[counter]
+0

它繼續給我這個運行時錯誤:線程「main」java.lang.ArrayIndexOutOfBoundsException中的異常:5 at Plumber。 (Plumber.java:52) 在位於這裏的Plumber.main(Plumber.java:76):totalHours + = hours [counter]; –

+0

檢查我對你的OP的評論......這將是因爲退出第一個循環時的計數器變量是5,而在你的小時[]數組中該位置沒有變量......即,沒有小時[5] ==第六元素 –

+0

@ H-Patel,我遵循你的建議,但仍然得到相同的運行時間錯誤... –