使用二维数组实现存储学生成绩

需求:1、定义一个20*5的二维数组,用来存储某班级20位学员的5门课的成绩;这5门课 按存储顺序依次为:core,C++,coreJava,Servlet,JSP和EJB

2、循环给二维数组的每一个元素赋0~100之间的随机整数。3、按照列表的方式输出这些学员的每门课程的成绩。4、要求编写程序求每个学员的总分,将其保留在另外一个一维数组中。5、要求编写程序求所有学员的某门课程的平均分。

import java.util.Random;
public class StudentScore {

	public static void main(String[] args) {
		int[][] arr = new int[20][5];// 定义一个二维数组
		Random rd = new Random();// 创建Random对象
		int[] totalScores = new int[20];// 定义一个一维数组用于存储每个学员的总分
		int[] avgClassScores = new int[5];// 定义一个数组存储所有学员某门课程的平均分
		System.out.println("            \t学员成绩表");
		System.out.println("学员编号\t" + "coreC++\t" + "coreJava" + "Servlet\t"
				+ "JSP\t" + "EJB\t" + "总成绩");
		for (int i = 0; i < arr.length; i++) {
			System.out.print((i + 1) + "\t");//学员编号
			for (int j = 0; j < arr[i].length; j++) {
				arr[i][j] = rd.nextInt(80) + 20;//生成成绩(为了好看,最少20分以上吧)
				totalScores[i] += arr[i][j];//加入总成绩数组
				System.out.print(arr[i][j] + "\t");//打印该门成绩
				avgClassScores[j] += arr[i][j];//加入单科成绩数组
			}
			System.out.print(totalScores[i] + "\n");
			System.out
					.println("———————————————————————————————————————————————————————");
		}
		System.out.print("平均分");
		for (int x = 0; x < avgClassScores.length; x++) {
			System.out.print("\t" + avgClassScores[x] /20.0);//输出打印平均成绩
		}
	}
}

运行结果图:ps:由于20个学生数据较长,截图不能全部截下,这里为了演示结果,临时改为10个。

使用二维数组实现存储学生成绩_第1张图片

考虑到想进一步实现按照总成绩高低顺序输出,代码改进如下:

public class StudentScore {
	private int coreC_Score;
	private int coreJava_Score;
	private int Servlet_Score;
	private int JSP_Score;
	private int EJB_Score;
	private int totalScores;
	int arr[];

	public StudentScore(int[] arr) {
		this.arr = arr;
		this.coreC_Score = arr[0];
		this.coreJava_Score = arr[1];
		this.Servlet_Score = arr[2];
		this.JSP_Score = arr[3];
		this.EJB_Score = arr[4];
		this.totalScores = arr[5];// 总成绩
	}

	public int getCoreC_Score() {
		return coreC_Score;
	}

	public void setCoreC_Score(int coreC_Score) {
		this.coreC_Score = coreC_Score;
	}

	public int getCoreJava_Score() {
		return coreJava_Score;
	}

	public void setCoreJava_Score(int coreJava_Score) {
		this.coreJava_Score = coreJava_Score;
	}

	public int getServlet_Score() {
		return Servlet_Score;
	}

	public void setServlet_Score(int servlet_Score) {
		Servlet_Score = servlet_Score;
	}

	public int getJSP_Score() {
		return JSP_Score;
	}

	public void setJSP_Score(int jSP_Score) {
		JSP_Score = jSP_Score;
	}

	public int getEJB_Score() {
		return EJB_Score;
	}

	public void setEJB_Score(int eJB_Score) {
		EJB_Score = eJB_Score;
	}

	public int getTotalScores() {
		return totalScores;
	}

	public void setTotalScores(int totalScores) {
		this.totalScores = totalScores;
	}

}

接下来,写一个比较类

import java.util.Comparator;

public class MyCompare implements Comparator {

	@Override
	public int compare(StudentScore o1, StudentScore o2) {

		return Integer.valueOf(o2.getTotalScores()).compareTo(
				Integer.valueOf(o1.getTotalScores()));

	}

}

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class StudentScore2{

	public static void main(String[] args) {
		List list = new ArrayList();//创建ArrayList集合
		int[] arr = new int[6];
		int[] avgClassScores = new int[5];// 定义一个数组存储所有学员某门课程的平均分
		Random rd = new Random();// 创建Random对象
		for (int i = 1; i <= 20; i++) {
			int sum = 0;
			for (int j = 0; j <= 4; j++) {
				arr[j] = rd.nextInt(80) + 20;//生成成绩
				sum += arr[j];
				avgClassScores[j] += arr[j];
			}
			arr[5] = sum;
			StudentScore stus = new StudentScore(arr);
			list.add(stus);
		}
		Collections.sort(list, new MyCompare());//根据比较器排序
		System.out.println("            \t学员成绩表");
		System.out.print("学员编号\t" + "coreC++\t" + "coreJava" + "Servlet\t"
				+ "JSP\t" + "EJB\t" + "总成绩\t\n");
		for (int i = 0; i < 20; i++) {
			System.out.print((i + 1) + "\t");
			StudentScore studentScore = list.get(i);
			System.out.print(studentScore.getCoreC_Score() + "\t");
			System.out.print(studentScore.getCoreJava_Score() + "\t");
			System.out.print(studentScore.getEJB_Score() + "\t");
			System.out.print(studentScore.getJSP_Score() + "\t");
			System.out.print(studentScore.getServlet_Score() + "\t");
			System.out.print(studentScore.getTotalScores() + "\n");
			System.out
					.println("———————————————————————————————————————————————————————");
		}

		System.out.print("平均分");
		for (int x = 0; x < avgClassScores.length; x++) {
			int avg = Math.round(avgClassScores[x] / 20);
			System.out.print("\t" + avg);
		}

	}
}

运行结果图:实现了按成绩由高到低的排序输出。

使用二维数组实现存储学生成绩_第2张图片

你可能感兴趣的:(Java编程题练习)