package com.test.demo;
import java.util.*;
public class StudentRecord {
public static void main(String[] args){
try {
print("请输入学生人数:");
Scanner stuNumSc = new Scanner(System.in);
Integer stuNum = stuNumSc.nextInt();
print("请输入科目数量:");
Scanner subNumSc = new Scanner(System.in);
Integer subNum = subNumSc.nextInt();
Map<Student, List<Course>> studentRescords = new TreeMap<Student, List<Course>>(
new Comparator<Student>() {
public int compare(Student obj1, Student obj2) {
// 降序排序
return obj2.getTotalRecord().compareTo(obj1.getTotalRecord());
}
});
List<Course> listCourse = new ArrayList<Course>(subNum);
for(int i = 0;i<subNum;i++){
print("请定义第"+(i+1)+"门课程的名字");
Scanner subNameSc = new Scanner(System.in);
Course c = new Course();
c.setName(subNameSc.next());
listCourse.add(c);
}
for(int i=0;i<stuNum;i++){
print("请输入学生的姓名");
Scanner stuNameSc = new Scanner(System.in);
Student stu = new Student();
stu.setName(stuNameSc.next());
List<Course> courseLists = new ArrayList<Course>(subNum);
for(int j=0;j<subNum;j++){
print("请输入学生"+stu.getName()+listCourse.get(j).getName()+"的成绩");
Scanner recordSc = new Scanner(System.in);
Double record = recordSc.nextDouble();
Course c = new Course();
c.setName(listCourse.get(j).getName());
c.setRecord(record);
courseLists.add(c);
}
Double totalRec = 0.00;
for(int m = 0 ;m<subNum;m++){
totalRec= totalRec + courseLists.get(0).getRecord();
}
Double avgRec = totalRec/subNum;
stu.setAvgRecord(avgRec);
stu.setTotalRecord(totalRec);
studentRescords.put(stu,courseLists);
}
StringBuffer sb = new StringBuffer();
for(int i=0;i<listCourse.size();i++){
sb.append(" ").append(listCourse.get(i).getName());
}
System.out.println("学生 "+sb.toString()+" 总分"+" 平均分"+" 排行榜");
Set<Student> keySet = studentRescords.keySet();
Iterator<Student> iter = keySet.iterator();
int re = 1;
while (iter.hasNext()) {
Student key = iter.next();
List<Course> lists = studentRescords.get(key);
StringBuffer sbStu = new StringBuffer();
for(int i=0;i<lists.size();i++){
sbStu.append(" ").append(lists.get(i).getRecord());
}
System.out.println(key.getName()+" "+sbStu.toString()+" "+key.getTotalRecord()+" "+key.getAvgRecord()+" 第"+re+"名");
re+=1;
}
}catch(Exception e){
print("输入非法,即将退出");
try {
Thread.sleep(1000L);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.exit(0);
}
}
static void print(String text){
System.out.println(text);
}
static class Student{
private String name;
private Double totalRecord;
private Double avgRecord;
public void setName(String name){
this.name = name;
}
public void setTotalRecord(Double totalRecord){
this.totalRecord = totalRecord;
}
public void setAvgRecord(Double avgRecord){
this.avgRecord = avgRecord;
}
public String getName(){
return this.name;
}
public Double getTotalRecord(){
return this.totalRecord;
}
public Double getAvgRecord(){
return this.avgRecord;
}
}
static class Course{
private String name;
private Double record;
public void setName(String name){
this.name = name;
}
public void setRecord(Double record){
this.record = record;
}
public String getName(){
return this.name;
}
public Double getRecord(){
return record;
}
}
}
你可以将对应的数据进行倒序排序后,添加一个自增序列的字段就可以了。
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632