十四章(例四)

package zxxsa;

import java.util.*;

public class shilisi {
	Scanner input = new Scanner(System.in);
	int[] scores = new int[5];

	public double calAvg(int[] scores) {
		int sum = 0;
		double avg = 0.0;
		for (int i = 0; i < scores.length; i++) {
			sum += scores[i];
		}
		avg = (double) sum / scores.length;
		return avg;
	}

	public int calMax(int[] scores) {
		int max = scores[0];
		for (int i = 1; i < scores.length; i++) {
			if (max < scores[i]) {
				max = scores[i];

			}
		}
		return max;
	}
}



package zxxsa;

import java.util.*;

public class shilisi1 {
	static Scanner input = new Scanner(System.in);

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		shilisi jr = new shilisi();
		int[] scores = new int[5];
		System.out.println("请输入五名参赛者的成绩");
		for (int i = 0; i < 5; i++) {
			System.out.println("请输入第" + (i + 1) + "个学生的成绩");
			scores[i] = input.nextInt();
		}
		double avgScore = jr.calAvg(scores);
		System.out.println("平均成绩" + avgScore);
		int maxScore = jr.calMax(scores);
		System.out.println("最高分数" + maxScore);
	}

}


 
  

你可能感兴趣的:(十四章(例四))