12、(List)在控制台输入格式为“张三/18/男/99.5”的学生若干,存于List集合中。

发布时间 2023-04-20 17:57:59作者: ZuaMagee

要求:

①从数组遍历所有内容解析为学生对象,将学生在存于一个新的List集合

②遍历集合找出”优秀”的学生信息(优秀:成绩>=80)

③找出集合中没有参加考试的学生信息(成绩为null)

④制定成绩光荣榜(成绩从高到低打印学生成绩)

import java.util.*;

/**
 * @author Spring-_-Bear
 * @version 2021-10-31 11:45
 */

public class Main {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>(5);
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            String str = scanner.next();
            students.add(change(str));
        }
        List<Student> list = new ArrayList<>(students);
        ArrayList<Double> score = new ArrayList<>();
        for (Student s : list) {
            if (s.getScore() == null) {
                System.out.println(s);
            } else {
                score.add(s.getScore());
            }
        }
        score.sort(((o1, o2) -> o2.compareTo(o1)));
        for (Double s : score) {
            System.out.println(s);
        }

    }
    public static Student change(String str) {
        Student student = new Student();
        String[] split = str.split("/");
        student.setName(split[0]);
        student.setAge(Integer.parseInt(split[1]));
        student.setSex(split[2]);
        if (split.length < 4) {
            return student;
        }
        student.setScore(Double.parseDouble(split[3]));
        return student;
    }
}