1创建10个学生对象(其中有两个学员信息完全相同)存入集合中 2去除重复的学员并遍历打印剩余学员信息3打印最高分的学员信息 4不及格的学员信息及数量

package day07;
/*
第四题:分析以下需求,并用代码实现
定义Student类包含姓名:String name、年龄:int age、成绩:int score,生成空参、有参构造、set和get方法、toString方法
1.创建10个学生对象(其中有两个学员信息完全相同)存入集合中
2.去除重复的学员并遍历打印剩余学员信息(分别利用普通for循环、增强for循环、迭代器遍历)
3.打印最高分的学员信息
4.打印平均分
5.打印不及格的学员信息及数量
*/

import java.util.*;

public class Test04 {
    public static void main(String[] args) {
        Student s1 = new Student("王小一", 10, 100);
        Student s2 = new Student("王小二", 12, 98);
        Student s3 = new Student("王小三", 13, 34);
        Student s4 = new Student("王小四", 14, 56);
        Student s5 = new Student("王小五", 15, 87);
        Student s6 = new Student("王小六", 16, 90);
        Student s7 = new Student("王小七", 18, 64);
        Student s8 = new Student("王小八", 18, 98);
        Student s9 = new Student("王小九", 19, 66);
        Student s10 = new Student("王小一", 10, 100);
        TreeSet hs = new TreeSet<>(new Comparator() {
            @Override
            public int compare(Student s1, Student s2) {
                int i1 = s2.getScore() - s1.getScore();
                int i2 = i1 == 0 ? s2.getAge() - s1.getAge() : i1;
                int i = i2 == 0 ? s1.getName().compareTo(s2.getName()) : i2;
                return i;
            }
        });
        hs.add(s1);
        hs.add(s2);
        hs.add(s3);
        hs.add(s4);
        hs.add(s5);
        hs.add(s6);
        hs.add(s7);
        hs.add(s8);
        hs.add(s9);
        hs.add(s10);
        ArrayList al = new ArrayList<>();
        for (Student h : hs) {
            al.add(h);
        }
        //三种遍历方式
        for (Student s : al) {
            System.out.println(s);
        }
        System.out.println("===============");
        for (int i = 0; i < al.size(); i++) {
            System.out.println(al.get(i));
        }
        System.out.println("=============");
        Iterator it = al.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        System.out.println("============");
//        打印最高分的学员信息
        for (Student s : al) {
            System.out.println(s);
            break;
        }
        System.out.println("===================");
        //打印平均分
        int sum = 0;
        for (Student s : al) {
            sum += s.getScore();
        }
        System.out.println(sum * 1.0 / al.size());
        System.out.println("===================");
//打印不及格的学员信息及数量
        int count = 0;
        for (Student s : al) {
            if (s.getScore() < 60) {
                System.out.println(s);
                count++;
            }
        }
        System.out.println("不及格人数为" + count);

    }
}

class Student {
    private String name;
    private int age;
    private int score;

    public Student() {
    }

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

}

你可能感兴趣的:(代码案例)