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");}
}