2011-01-14 181 views
0

我正在嘗試創建一個計算球員平均分數,團隊平均分和團隊中最高平均分的程序。錄製個人平均和累計平均分和最高平均分

問題:

  1. 我無法弄清楚如何顯示所有在最後個別球員的平均值,而不顯示每次循環迭代。
  2. 我該如何設置代碼才能計算累計平均值和最高平均值?

下面是我寫到目前爲止

import java.util.*; 

public class bowlerama2 
{ 
    public static void main(String args[]) 
    { 
     Scanner keyboard=new Scanner(System.in); 
     int players;// for number of players on the team 
     int games=0;// for number of games played 
     int totalGames=0; 
     System.out.println("Enter the number of players on the bowling team."); 
     System.out.println("You must enter a number greater than or equal to three"); 
     players=keyboard.nextInt(); 

     //input validation 
     while (players<3) 
     { 
      System.out.println("Invalid entry. You must enter a number greater than or equal to three for the number of players on the team."); 
      System.out.println("Enter the number of players on the bowling team:"); 
      players=keyboard.nextInt(); 
     } 


     for(int number=1;number<=players;number++) 
     { 
      System.out.println("Enter the number of games bowled by player " + number + ":"); 
      System.out.println("You must enter a number greater than or equal to two."); 
      games=keyboard.nextInt(); 
      while (games<2) 
      { 
       System.out.println("Invalid entry.You must enter a number greater than or equal to two for the number of games played"); 
       System.out.println("Enter the number of games bowled by player " +number+ ":"); 
       games=keyboard.nextInt(); 
      } 
      totalGames+=games; 

      receive (number, games, totalGames); 

     } 

    } 
    public static void receive (int number, int games, int totalGames) 
    { 
     Scanner keyboard= new Scanner(System.in); 
     int total=0;//accumulator for individual scores 
     int score=0; 
     int average=0; 


     for (int amount=1;amount<=games;amount++) 
     { 

      System.out.println("Enter the score for player " +number+ " in game " + amount + ":"); 
      score=keyboard.nextInt(); 

      //input validation 
      while(score<0 || score>300) 
      { 
       System.out.println("Invalid entry. Score must be greater than or equal to zero and less than or equal to three hundred"); 
       System.out.println("Enter the score for player " + number+ " in game " +amount+ ":"); 
       score=keyboard.nextInt(); 
      } 

      total+=score; 
      average=total/amount; 

      System.out.println("The average score for player " +number+ " is:" + average); 
     } 




    } 

} 
+0

無關:格式化壞了。 – Nishant 2011-01-14 20:32:44

+0

@Nishant修正它 – marcog 2011-01-14 20:33:05

回答

0

1)存儲平均值在一個ArrayList每個玩家的代碼。所以當你計算出平均值時,你只需把它放在列表中並且已經存在。

ArrayList arrayList = new ArrayList(); 
arrayList.add(new Integer(average)); 

然後訪問列表中的玩家的得分主要方法你想

2)對於累計平均只迭代ArrayList增加分數在一起,然後通過ArrayList的長度劃分。對於最高分數,如果設置爲等於新的最高平均分數,則使用if語句來檢查每個玩家的平均分數是否高於之前玩家的最高平均分數。

+0

爲了訪問主方法中的列表中應該在 – trs 2011-01-14 20:54:18

+0

中聲明數組列表的方法你要麼想在你的main中聲明列表,然後將它傳遞給receive,要麼使它成爲class var。我建議僅僅因爲其更好的做法而通過它。 – Grammin 2011-01-14 21:01:49

0

您將需要使用某種臨時數據存儲。諸如數組或ArrayList(http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html)。

List al = new ArrayList<Type>(); 

對於你的情況類型將是整數。一旦你將所有的東西都存儲在一個ArrayList中,那麼你可以循環訪問ArrayList來合併所有的值。同樣爲了找到「最大值」,您將需要初始設置一個整數變量爲-999。然後遍歷所有值,如果值> max,則將該變量設置爲max。

0

我打算從代碼猜測,到目前爲止,你還沒有覆蓋的ArrayList着呢,如果你提交一個解決方案,它使用它,你的老師可能會有點可疑:-)看起來也許一些簡單的數組會有所幫助。您需要首先獲取玩家人數,以便創建適當大小的陣列。使用一個數組來存儲每個玩家的累積分數,並使用一個數組來存儲每個玩家的分數。在收集輸入時填充數組值。然後存儲的值可以讓你計算每個球員的平均分數,球隊平均分和最高球員平均分。

的關鍵是,有輸入階段時,你填充陣列,然後計算階段(在這裏你通過數組迭代,並執行caculations),然後在顯示階段。

0
  1. avarage分數爲每個玩家。你可以通過字符串連接來完成。首先使空的靜態字符串。每次接收後將新數據附加到字符串。播放器循環後的println結果字符串。
  2. 團隊avarage和最高avarage。

    team_av = SUM(player_av)/球員

在main方法

double sum_player_avarage=0 
    double highest_avarage=0 
在您收到方法

返回球員avarage的雙重價值。

double player_av= receive (number, games, totalGames); 
if (highest_avarage<player_av) highest_avarage=player_av; 
sum_player_avarage+=player_av; 

畢竟:

team_avarage=sum_player_avarage/players; 

println(team_avarage); 
println(highest_avarage) 
println(StringWithPlayersAvarage)