我正在嘗試創建一個計算球員平均分數,團隊平均分和團隊中最高平均分的程序。錄製個人平均和累計平均分和最高平均分
問題:
- 我無法弄清楚如何顯示所有在最後個別球員的平均值,而不顯示每次循環迭代。
- 我該如何設置代碼才能計算累計平均值和最高平均值?
下面是我寫到目前爲止
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