Java数据结构课程设计 学生成绩管理

题目:给出n 个学生的考试成绩表,每条信息由姓名与分数组成,试设计一个算法(1)按分数高低次序打印出每个学生在考试中获得的名次,分数相同的为同一名次;(2)按名次列出每个学生的姓名和分数。其中,学生的考试成绩表必须通过键盘输入数据而建立,同时要对输出进行格式控制。用冒泡排序或快速排序算法实现该问题,最后要对结果做简单分析。

import java.util.*;

public class StudentScoreManagement {
	private Scanner reader = new Scanner(System.in);
	private ArrayList students = new ArrayList();

	public static void main(String[] args) {
		StudentScoreManagement ssm = new StudentScoreManagement();
		ssm.input();
		ssm.ranking();
		System.out.println("1.按分数高低次序打印每个学生在考试中获得的名次");
		System.out.println("2.按名次列出每个学生的姓名和分数");
		System.out.println("请选择:");
		Scanner choice = new Scanner(System.in);
		int ch = choice.nextInt();
		while(true) {
			if (ch == 1) {
				ssm.printSortedByScore();
				break;
			}
			else if (ch == 2) {
				ssm.printSortedByRank();
				break;
			}
			else {
				System.out.println("选择错误...");
				System.out.println("1.按分数高低次序打印每个学生在考试中获得的名次");
				System.out.println("2.按名次列出每个学生的姓名和分数");
				System.out.println("请选择:");
				ch = choice.nextInt();
			}
		}
		// System.out.println(ssm.getStudents());
	}

	//public ArrayList getStudents() {
	//	return students;
	//}
	private void bubbleSort() {
		Student a, b;
		for (int i = students.size() - 1; i > 0; i--) {
			for (int j = 0; j < i; j++) {
				a = students.get(j);
				b = students.get(j+1);
				if (a.getScore() < b.getScore()) {
					swap(j, j+1);
				}
				else if (a.getScore() == b.getScore()) {
					if (a.getName().compareTo(b.getName()) > 0) {
						swap(j, j+1);
					}
				}
			}
		}
	}

	private void swap(int i, int j){
		Student tmp = students.get(i);
		students.set(i, students.get(j));
		students.set(j, tmp);
	}

	public void input() {
		String name;
		int score, n;
		System.out.print("Enter the number of student: ");
		n = reader.nextInt();
		for(int i = 0; i < n; i++){
			System.out.println(i == n - 1 ? "Last one" : i + 1 + ":");
			System.out.print("Enter student's name: ");
			name = reader.next();
			System.out.print("Enter student's score: ");
			score = reader.nextInt();
			students.add(new Student(name, score));
		} 
	}

	public void ranking() {
		//Collections.sort(students);
		bubbleSort();
		int j = 1;
		Student stmp =students.get(0);
		stmp.setRank(j);
		for(int i = 1; i {
	private String name;
	private int score;
	private int rank;
	public Student(String name, int score) {
		this.name = name;
		this.score = score;
	}

	public int compareTo(Student stu) {
		int tmp = stu.getScore() - this.score;
		if (tmp == 0)
			return this.name.compareTo(stu.getName());
		else
			return tmp;
	}

	public String getName() {
		return name;
	}

	public int getScore() {
		return score;
	}

	public int getRank() {
		return rank;
	}

	public void setRank(int n) {
		this.rank = n;
	}
}


你可能感兴趣的:(Java)