7-5 sdust-Java-学生成绩读取与排序 (22分)

1)从键盘录入多行学生成绩的信息,每行表示一个学生的一门课的成绩,最后一行以“exit”结束。每行文本的格式为:学号,姓名,课程名,成绩。程序能够读取学生及其成绩,将具有相同学号的学生及其成绩读取到一个Student(学生类)类对象的列表(List)stuList中; 2)程序在读取完学生及其成绩的数据后,能够将stuList中的学生按照平均成绩降序排列(如果平均成绩相同,学号数字小的排在前面), 并输出排序后的学生学号、姓名和成绩。
输入格式:

多行表示的学生成绩,每一行是一个学生的姓名、学号、课程名、成绩(整数)。不同行可以是同一名学生(学号相同的为同一名学生)不同课程的成绩。
输出格式:

按照学生平均成绩降序排序(平均成绩相同的学号小的在前面)的学生排名(具体输出格式参照样例)。
输入样例:

小明,2001,Java,88
小刚,2002,Java,78
小丁,2003,Java,56
小宏,2004,Java,85
小明,2001,Python,84
小刚,2002,Python,98
小丁,2003,JavaWeb,66
小宏,2004,Algorithm,87
exit

输出样例:

No1:2002,小刚
No2:2001,小明
No3:2004,小宏
No4:2003,小丁

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
    	Scanner se=new Scanner (System.in);
        ArrayList<student> z=new ArrayList<student>();
        int t=1;
        while(se.hasNext()) {
        	String s=se.nextLine();
        	if(!s.equals("exit")) {
        		if (t==1) {
            		String a[]=s.split(",");
            		z.add(new student(a[0],Integer.parseInt(a[1]),a[2],Integer.parseInt(a[3])));
            		t++;
            	}else {
            		String a[]=s.split(",");
            		boolean b=false;
            		student w=new student(a[0],Integer.parseInt(a[1]),a[2],Integer.parseInt(a[3]));
            		for(student r:z) {
            			if(w.equals(r)& !w.getKecheng().equals(r.getKecheng()) ) {
            				r.jiachengji(w.getChengji());
            				r.kechenggeshu++;
            				b=true;
            			}
            		}
            		if(b==false) {
            			z.add(w);
            		}
            	}
        	}else {
        		break;
        	}
        }
        Collections.sort(z, new gongjv());
        for(student y:z) {
        	System.out.println(y.toString());
        }
    }
}

class gongjv implements Comparator<student> {
	public int compare(student  p1,student p2) {
		if(p1.qiupingjunchengji()>p2.qiupingjunchengji()) {
			return -1;
		}else if(p1.qiupingjunchengji()<p2.qiupingjunchengji()) {
			return 1;
		}else {
			if(p1.getXuehao()>p2.getXuehao()) {
				return 1;
			}else if(p1.getXuehao()<p2.getXuehao()) {
				return -1;
			}else {
				return 0;
			}
		}
	}
}
class student {
	private int xuehao,chengji;
	int kechenggeshu=0;
	private String kecheng,xingming;
	public static int no=0;
	@Override
	public String toString() {
		no++;
		return "No"+no+":"+xuehao+","+xingming  ;
	}
	public student( String xingming,int xuehao, String kecheng,int chengji) {
		super();
		this.xuehao = xuehao;
		this.chengji = chengji;
		this.kecheng = kecheng;
		this.xingming = xingming;
		kechenggeshu++;
	}
	public int qiupingjunchengji() {
		return this.chengji/this.kechenggeshu;
	}
	public void jiachengji(int k) {
		this.chengji=this.chengji+k;
	}
	public int getXuehao() {
		return xuehao;
	}
	public void setXuehao(int xuehao) {
		this.xuehao = xuehao;
	}
	public int getChengji() {
		return chengji;
	}
	public void setChengji(int chengji) {
		this.chengji = chengji;
	}
	public String getKecheng() {
		return kecheng;
	}
	public void setKecheng(String kecheng) {
		this.kecheng = kecheng;
	}
	public String getXingming() {
		return xingming;
	}
	public void setXingming(String xingming) {
		this.xingming = xingming;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((xingming == null) ? 0 : xingming.hashCode());
		result = prime * result + xuehao;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		student other = (student) obj;
		if (xingming == null) {
			if (other.xingming != null)
				return false;
		} else if (!xingming.equals(other.xingming))
			return false;
		if (xuehao != other.xuehao)
			return false;
		return true;
	}
	
}

版权声明:本文为sddfnsff原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。