我正在尝试创建一个程序来计算投球手的平均得分,球队平均得分以及球队的最高平均得分。
问题:
以下是我到目前为止编写的代码
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 :(得分:0)
1)将每个玩家的平均值存储在一个arraylist中。因此,当你计算平均值时,你只需将它扔到列表中就可以了。
ArrayList arrayList = new ArrayList();
arrayList.add(new Integer(average));
然后访问主方法中的列表,以获取您想要的玩家分数
2)对于累积平均值,只需迭代arrayList,将得分加在一起,然后除以arrayList长度。要获得最高分,请使用if语句检查每位玩家的平均得分是否高于之前玩家的最高平均得分,如果设置为等于新的最高平均得分。
答案 1 :(得分:0)
您将需要使用某种临时数据存储。诸如Array或ArrayList之类的东西(http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html)。
List al = new ArrayList<Type>();
对于你的情况,Type将是Integer。一旦将所有内容存储在ArrayList中,您就可以循环遍历ArrayList以添加所有值。另外要找到“max”,您需要将整数变量设置为-999。然后运行所有值,如果值>&gt; max然后将该变量设置为max。
答案 2 :(得分:0)
到目前为止,我将从代码中猜测你还没有覆盖ArrayList,如果你提交一个使用它的解决方案,你的老师可能会有点怀疑:-)看起来好像是一个几个简单的数组会有所帮助。您需要先获取玩家数量,以便创建适当大小的阵列。使用一个阵列存储每个玩家的累积分数,使用一个阵列存储每个玩家的分数。在收集输入时填充数组值。然后,存储的值应该允许您计算每个球员的平均得分,球队平均值和最高球员平均值。
关键是你有填充数组的输入阶段,然后是计算阶段(你遍历数组并执行计算),然后是显示阶段。
答案 3 :(得分:0)
获得团队平均水平和最高平均水平。
team_av =总和(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)