Java—stream以及集合框架使用

1) 编写Student类,主要属性包括学号、姓名、性别、班级
2) 编写Score类,主要属性包括:学号、课程名、分数
3) 模拟期末考试的成绩统计应用场景,要求
(1) 所有学生名单及对应科目成绩已经初始化在数组中
(2) 要求输出每门课程的所有学生的平均成绩。
(3) 输出某门课程的最高成绩,最低成绩
(4) 可以查询某个学生的所有成绩。

源代码

二、源程序
Score类
public class Score {

    private String studentID;
    private String course;
    private int grades;

    public Score(String studentID, String course, int grades) {
        this.studentID = studentID;
        this.course = course;
        this.grades = grades;
    }

    public String getStudentID() {
        return studentID;
    }

    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    public int getGrades() {
        return grades;
    }

    public void setGrades(int grades) {
        this.grades = grades;
    }
}

Student类
public class Student {

   private String studentID;
   private String studentName;
   private String sex;
   private String classID;

   public Student(String studentID, String studentName, String sex, String classID) {
      this.studentID = studentID;
      this.studentName = studentName;
      this.sex = sex;
      this.classID = classID;
   }

   public String getStudentID() {
      return studentID;
   }

   public void setStudentID(String studentID) {
      this.studentID = studentID;
   }

   public String getStudentName() {
      return studentName;
   }

   public void setStudentName(String studentName) {
      this.studentName = studentName;
   }

   public String getSex() {
      return sex;
   }

   public void setSex(String sex) {
      this.sex = sex;
   }

   public String getClassID() {
      return classID;
   }

   public void setClassID(String classID) {
      this.classID = classID;
   }
}

Util类
public class Util {
    public static void getAverage(Score[] scores) {

        Set<String> set = new HashSet<>();
        for (Score score : scores)
            set.add(score.getCourse());
        for (String course : set)
            System.out.println(course + "平均成绩:" + Arrays.stream(scores).filter(x -> x.getCourse().equals(course)).mapToInt(Score::getGrades).summaryStatistics().getAverage());
    }

    public static void getMax(Score[] scores, String course) {
        IntSummaryStatistics statistics = Arrays.stream(scores).filter(x -> x.getCourse().equals(course)).mapToInt(Score::getGrades).summaryStatistics();
        System.out.println(course + "最高成绩:" + statistics.getMax());
        System.out.println(course + "最低成绩:" + statistics.getMin());
    }

    public static void getAllGrade(Score[] scores, Student[] students, String studentName) {
        String studentID = null;
        for (Student student : students)
            if (student.getStudentName().equals(studentName))
                studentID = student.getStudentID();


        for (Score score : scores)
            if (studentID.equals(score.getStudentID()))
                System.out.println(studentName + "  " + score.getCourse() + "成绩为:" + score.getGrades());
    }

    public static void main(String[] args) {
        Score[] scores = new Score[]{new Score("1", "math", 89), new Score("1", "english", 90), new Score("2", "math", 92), new Score("2", "english", 95)};
        Student[] student = new Student[]{new Student("1", "mary", "women", "1"), new Student("2", "john", "men", "1")};
        getAverage(scores);
        getMax(scores, "math");
        getAllGrade(scores, student, "john");

    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值