7-2 sdust-Java-学生成绩读取与排序 (20 分)
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.awt.*;
import java.util.*;
import javax.swing.JPanel;
import java.io.*;
class Student implements Comparable<Student> {
public String name;
public int no;
public String kc;
public int score;
public int numkc;
public ArrayList<Integer> scolist = new ArrayList<>();
public double avescore;
public Student(String name, int no, String kc, int score) {
super();
this.name = name;
this.no = no;
this.kc = kc;
this.score = score;
scolist.add(score);
avescore = 1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getKc() {
return kc;
}
public void setKc(String kc) {
this.kc = kc;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getNumkc() {
return numkc;
}
public ArrayList<Integer> getScolist() {
return scolist;
}
public double getAvescore() {
return avescore;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + no;
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 (no != other.no)
return false;
return true;
}
public void addscore(int scor) {
scolist.add(scor);
numkc++;
}
public void avesco() {
double sum = 0;
for (int i : scolist) {
sum += i;
}
avescore = sum / numkc;
}
public int compareTo(Student s1) {
if (this.avescore < s1.avescore) {
return 1;
} else if (this.avescore > s1.avescore) {
return -1;
} else {
return this.no - s1.no;
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
LinkedList<Student> stulist = new LinkedList<>();
while (scan.hasNext()) {
String str = scan.nextLine();
if ("exit".equals(str)) {
break;
}
String[] strr = str.split(",");
int a = Integer.parseInt(strr[1]);
int b = Integer.parseInt(strr[3]);
Student s1 = new Student(strr[0], a, strr[2], b);
if (!stulist.contains(s1)) {
stulist.add(s1);
} else {
for (Student s2 : stulist) {
if (s2.equals(s1)) {
// System.out.println(b);
s2.addscore(b);
break;
}
}
}
}
for (Student s3 : stulist) {
s3.avesco();
}
Collections.sort(stulist);
for (int i = 0; i < stulist.size(); i++) {
System.out.println("No" + (i + 1) + ":" + stulist.get(i).getNo() + "," + stulist.get(i).getName());
}
}
}