6-4 编程练习题答案

import java.util.Scanner;

public class Test
{
	final static int MAX = 100;
	public static void main(String[] args)
	{
		int[] scores = enterScores();
		judgeScores(scores);
	}

	public static int[] enterScores()
	{
		Scanner input = new Scanner(System.in);
		int[] scores = new int[MAX];
		System.out.print("Enter some scores: ");
		for(int i = 0; i < MAX; i++)
		{
			scores[i] = input.nextInt();
			if(scores[i] < 0)
				break;
		}
		return scores;
	}

	public static void judgeScores(int[] scores)
	{
		int sum = 0;
		int average;
		int realNumberCounts = 0;
		for(int i = 0; i < scores.length; i++)
		{
			if(scores[i] < 0)
				break;
			else
			{
				sum += scores[i];
				realNumberCounts++;
			}
				
		}
		average = sum / realNumberCounts;
		
		int countsBelowAverage = 0;
		int countsAboveAverage = 0;
		for(int i = 0; i < realNumberCounts; i++)
		{
			if(scores[i] < average)
				countsBelowAverage++;
			else
				countsAboveAverage++;
		}
		System.out.println("The average score is " + average);
		System.out.println("There are " + countsAboveAverage + " scores above and equal the average score.");
		System.out.println("There are " + countsBelowAverage + " scores below the average score.");
	}
}

你可能感兴趣的:(Java语言程序设计-基础篇)