java控制臺實現學生管理系統
本文實例為大傢分享瞭java控制臺實現學生管理系統的具體代碼,供大傢參考,具體內容如下
編寫一個學生成績管理程序。學生的屬性包括學號、姓名、年齡等。每個學生要學習若幹課程,每門課程有平時成績、期中考試成績、實習成績、期末考試成績以及總評成績等多個成績,其中平時成績的數目不定(因為不同課程的教師佈置的平時作業數不同),而總評成績是其它成績的平均值。請先設計合理的對象容器類存放這些信息,然後設計程序完成以下功能:
1)列出某個學生的所有成績;
2)列出某門課程每個學生的總評成績,及所有學生總評成績的總評分;
3)分區段統計某門課程的學生總評成績,例如60 分以下的學生人數、60 至70 分的學生人數等。
Student類:
package homework032002; import java.util.ArrayList; public class Student { private String name; private String no; private int age; private ArrayList<Grade> Grade; public ArrayList<Grade> getCourses() { return Grade; } public void setCourses(ArrayList<Grade> Grade) { this.Grade = Grade; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", no=" + no + ", age=" + age + ", course=" + Grade + "]"; } public Student(String name, String no, int age, ArrayList<Grade> Grade) { super(); this.name = name; this.no = no; this.age = age; this.Grade = Grade; } public Student() { super(); // TODO Auto-generated constructor stub } }
Function類:(功能封裝在此類中):
package homework032002; import homework032002.Grade; import java.util.ArrayList; public class Function { //static ArrayList<Grade> arrayList = new ArrayList<>(); //static ArrayList<Student> arrayList2 = new ArrayList<>(); private ArrayList<Student> arrayList2; public Function(ArrayList<Student> arrayList) { super(); this.arrayList2 = arrayList; } //根據名字搜索該學生的成績 public void show(String name) { for (Student s : arrayList2) { if(s.getName().equals(name)) { ArrayList<Grade> course = s.getCourses(); System.out.println("該學生的成績是:"); for(Grade grade : course) { System.out.println(grade); } break; } } } //列出某門課程每個學生的總評成績,及所有學生總評成績的總評分 public void showOneSum(String courseName) { double sumGrade = 0; int num = 0; for(Student student : arrayList2) { ArrayList<Grade> grades = student.getCourses(); for(Grade grade : grades) { if(courseName.equals(grade.getCourse())) { num++; sumGrade += grade.getSumGrade(); System.out.println(student.getName()+"的總評分是"+grade.getSumGrade()); } } } System.out.println("所有學生的總評分是:"+sumGrade/num); } //分區段統計某門課程的學生總評成績,例如60 分以下的學生人數、60 至70 分的學生人數等 public void selectGrade(String courseName) { int down60 = 0; int down60_70 = 0; for(Student student : arrayList2) { ArrayList<Grade> grades = student.getCourses(); for(Grade grade : grades) { if(courseName.equals(grade.getCourse())) { if(grade.getSumGrade() < 60) { down60++; }else if (grade.getSumGrade() > 60 || grade.getSumGrade() <70) { down60_70++; } } } } System.out.println("60分以下的有"+down60+"人"); System.out.println("60-70分以下的有"+down60_70+"人"); } }
Grade類:(課程類)
package homework032002; //每門課程有平時成績、期中考試成績、實習成績、期末考試成績以及總評成績等多個成績 public class Grade { private String course; private int dailyGrade; private int midGrade; private int doGrade; private int endGrade; public String getCourse() { return course; } public void setCourse(String course) { this.course = course; } public Grade(String course, int dailyGrade, int midGrade, int doGrade, int endGrade, double sumGrade) { super(); this.course = course; this.dailyGrade = dailyGrade; this.midGrade = midGrade; this.doGrade = doGrade; this.endGrade = endGrade; this.sumGrade = sumGrade; } public int getDailyGrade() { return dailyGrade; } public void setDailyGrade(int dailyGrade) { this.dailyGrade = dailyGrade; } private double sumGrade; public int getMidGrade() { return midGrade; } public void setMidGrade(int midGrade) { this.midGrade = midGrade; } public int getDoGrade() { return doGrade; } public void setDoGrade(int doGrade) { this.doGrade = doGrade; } public int getEndGrade() { return endGrade; } public void setEndGrade(int endGrade) { this.endGrade = endGrade; } public double getSumGrade() { return sumGrade; } public void setSumGrade(double sumGrade) { this.sumGrade = sumGrade; } @Override public String toString() { return "Grade [course=" + course + ", dailyGrade=" + dailyGrade + ", midGrade=" + midGrade + ", doGrade=" + doGrade + ", endGrade=" + endGrade + ", sumGrade=" + sumGrade + "]"; } }
測試類:
package homework032002; import java.util.ArrayList; public class Test { public static void main(String[] args) { ArrayList<Student> students = new ArrayList<>(); ArrayList<Grade> grades = new ArrayList<>(); students.add(new Student("小明", "12", 13, grades)); grades.add(new Grade("高數", 50, 95, 45, 78, 76)); grades.add(new Grade("語文", 50, 95, 45, 78, 76)); grades.add(new Grade("英語", 50, 95, 45, 78, 76)); grades.add(new Grade("計算機", 50, 95, 45, 78, 76)); ArrayList<Grade> grades1 = new ArrayList<>(); students.add(new Student("小紅", "12", 13, grades1)); grades1.add(new Grade("高數", 50, 95, 45, 78, 76)); grades1.add(new Grade("語文", 50, 95, 45, 78, 76)); grades1.add(new Grade("英語", 50, 95, 45, 78, 76)); grades1.add(new Grade("計算機", 50, 95, 45, 78, 76)); ArrayList<Grade> grades2 = new ArrayList<>(); students.add(new Student("小上", "12", 13, grades2)); grades2.add(new Grade("高數", 50, 95, 45, 78, 76)); grades2.add(new Grade("語文", 50, 95, 45, 78, 76)); grades2.add(new Grade("英語", 50, 95, 45, 78, 76)); grades2.add(new Grade("計算機", 50, 95, 45, 78, 76)); ArrayList<Grade> grades3 = new ArrayList<>(); students.add(new Student("小藍", "12", 13, grades3)); grades3.add(new Grade("高數", 50, 95, 45, 78, 76)); grades3.add(new Grade("語文", 50, 95, 45, 78, 76)); grades3.add(new Grade("英語", 50, 95, 45, 78, 76)); grades3.add(new Grade("計算機", 50, 95, 45, 78, 76)); Function function = new Function(students); function.show("小明"); function.showOneSum("計算機"); function.selectGrade("高數"); }}
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。