2011-01-14 141 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)