1. 程式人生 > >使用二維陣列實現儲存學生成績

使用二維陣列實現儲存學生成績

需求: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個。


考慮到想進一步實現按照總成績高低順序輸出,程式碼改進如下:

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<StudentScore> {

	@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<StudentScore> list = new ArrayList<StudentScore>();//建立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);
		}

	}
}

執行結果圖:實現了按成績由高到低的排序輸出。