我正在尝试创建一个计算球员平均分数,团队平均分和团队中最高平均分的程序。录制个人平均和累计平均分和最高平均分
问题:
- 我无法弄清楚如何显示所有在最后个别球员的平均值,而不显示每次循环迭代。
- 我该如何设置代码才能计算累计平均值和最高平均值?
下面是我写到目前为止
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);
}
}
}
无关:格式化坏了。 – Nishant 2011-01-14 20:32:44
@Nishant修正它 – marcog 2011-01-14 20:33:05