package com.student;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
public class TestDemo extends Student{
static TestDemo t=new TestDemo();
TreeSet<Student> stu=new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {//排序
if(o1.getTotal()>o2.getTotal()){
return 1;
}else {
return -1;
}
}
});
public void add(String name,float yu,float shu,float eng){
float total=(yu+shu+eng);
Student student=new Student(name,yu,shu,eng,total);
stu.add(student);
}
public void query(){
Iterator i=stu.iterator();
while (i.hasNext()){
System.out.println(i.next());
}
}
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
Scanner input = new Scanner(System.in);
System.out.println("请输入姓名:");
String name = input.next();
System.out.println("请输入语文成绩:");
float yu = input.nextFloat();
System.out.println("请输入数学成绩:");
float shu = input.nextFloat();
System.out.println("请输入英语成绩:");
float eng = input.nextFloat();
t.add(name, yu, shu, eng);
t.query();
}
}
}
JAVA键盘录入3个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出
最新推荐文章于 2024-07-19 17:37:28 发布