1.1 题目与要求
设计一个学生成绩排名系统
实现以下功能:
(1) 具备对成绩的管理功能(添加、删除、排序);
(2) 具备对成绩的统计功能(最高分,最低分,平均分,及格率等);
(3) 具备按学号、姓名查询成绩的功能;
(4) 具备处理解决学号重复问题 ;
备注:成绩记录以下信息:班级,学号,姓名,四门成绩(百分制)。
1.1.1 作品展示
(1)操作界面如下:
(2)功能显示:
(3)功能查看(其他功能复制代码自行查看哟)
1.2 本系统涉及的知识点
(1)Student类 Course类
(2)StudentList类
(3)switch条件语句
(4)集合List的实现类ArrayList
(5)if-else if-else条件语句
(6)while循环和for循环
1.3 放在一个包里面四个类
1.4代码如下
(1). 建立course类
public class Course { //学生成绩,四门成绩构建一个类 private int politics; private int math; private int english; private int computer; public Course() { } public Course(int politics, int math, int english, int computer) { this.politics = politics; this.math = math; this.english = english; this.computer = computer; } public int getPolitics() { return politics; } public void setPolitics(int politics) { this.politics = politics; } public int getMath() { return math; } public void setMath(int math) { this.math = math; } public int getEnglish() { return english; } public void setEnglish(int english) { this.english = english; } public int getComputer() { return computer; } public void setComputer(int computer) { this.computer = computer; } }
(2).建立student类
public class Student extends Course{ private String classname; private String name; private String number; private Course a; public String getClassname() { return classname; } public void setClassname(String classname) { this.classname = classname; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public Course getA() { return a; } public void setA(Course a) { this.a = a; } }
(3)建立studentList类
package CourseDesign; public class StudentList { private String name; private int sum; public StudentList(String name, int sum) { this.name = name; this.sum = sum; } public StudentList() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSum() { return sum; } public void setSum(int sum) { this.sum = sum; } }
(4)建立studentmanager主类
import java.util.ArrayList; import java.util.Scanner; public class StudentManager { public static void main(String[] args) { //创建ArrayList集合,储存Student类型变量 ArrayList<Student> arry1=new ArrayList<Student>(); //创建ArrayList集合,储存Course类型变量 ArrayList<Course> arry2=new ArrayList<Course>(); //创建ArrayList集合,储存StudentList类型变量 ArrayList<StudentList> arry3=new ArrayList<StudentList>(); while(true) { System.out.println("------欢迎来到学生成绩排名系统------"); System.out.println("1 添加学生成绩,并且查看录入结果"); System.out.println("2 删除学生成绩"); System.out.println("3 对学生成绩排序,并且输出最高成绩和最低成绩"); System.out.println("4 查看平均分与及格率"); System.out.println("5 按学号、姓名、或课程名查询成绩"); System.out.println("6 查看学生成绩录入结果(无序)"); System.out.println("7 退出"); System.out.println("请输入你的选择:"); Scanner sc=new Scanner(System.in); int num=sc.nextInt(); //switch语句完成相关操作 switch(num) { case 1: //System.out.println("1 添加学生成绩"); addStudent(arry1,arry2); break; case 2: //System.out.println("2 删除学生成绩"); DeleteStudent(arry1); break; case 3: //System.out.println("3 对学生成绩排序,并且输出最高成绩和最低成绩"); sortStudent(arry1,arry3); break; case 4: //System.out.println("4 查看平均分与及格率"); averagStudent(arry1,arry2); break; case 5: //System.out.println("5 按学号、姓名、或课程名查询成绩"); searchStudent(arry1,arry2); break; case 6: //System.out.println("查看学生成绩录入结果(无序)"); lookStudent(arry1,arry2); break; case 7: System.out.println("谢谢使用"); System.exit(0); //终止当前正在运行的Java虚拟机 } } } //添加学生成绩,并且查看录入结果 public static void addStudent( ArrayList<Student> arry1,ArrayList<Course> arry2) { Scanner o=new Scanner(System.in); System.out.println("请输入要录入几个学生信息"); int num=o.nextInt(); for(int j=0;j<num;j++) { System.out.println("第" + (j + 1) + "位同学成绩录入"); Scanner sc = new Scanner(System.in); //为了让学号number能够循环外面被访问到,把它定义在循环外面 String number; //为了当学号输入重复,程序能回到这里,使用循环 while(true) { System.out.println("输入学生学号"); number = sc.nextLine(); boolean flag = isUsed(arry1, number); if (flag) { System.out.println("学号重复请重新输入"); } else { break; } } System.out.println("输入学生姓名"); String name = sc.nextLine(); System.out.println("输入学生班级"); String classmate = sc.nextLine(); System.out.println("输入政治成绩"); int a = sc.nextInt(); System.out.println("输入数学成绩"); int b = sc.nextInt(); System.out.println("输入英语成绩"); int d = sc.nextInt(); System.out.println("输入计算机成绩"); int e = sc.nextInt(); //建立学生对象,把键盘录入的数值赋值给学生成员变量 Student stu = new Student(); stu.setNumber(number); stu.setName(name); stu.setClassname(classmate); //建立四门成绩对象,把键盘录入的数值赋值给成员变量 Course c = new Course(); c.setPolitics(a); c.setMath(b); c.setEnglish(d); c.setComputer(e); //将引用变量添加到集合里面 arry2.add(c); stu.setA(c); arry1.add(stu); } } //解决学号重复问题 public static boolean isUsed(ArrayList<Student> arry1,String number) { boolean flag=false; for(int i=0;i<arry1.size();i++) { Student stu=arry1.get(i); if(stu.getNumber().equals(number)) { flag=true; break; } } return flag; } //删除学生成绩 public static void DeleteStudent(ArrayList<Student> arry1) { Scanner sc=new Scanner(System.in); System.out.println("请输入要删除学生的学号"); String number=sc.nextLine(); int index=-1; //设置索引判断学号是否存在 for(int i=0;i<arry1.size();i++) { //如果输入的学号和系统学生集合里面的学号有相同的 Student s = arry1.get(i); if (s.getNumber().equals(number)) { index=i; break; } } if(index==-1) { System.out.println("输入学号不存在,请重新输入"); }else { arry1.remove(index); System.out.println("删除学生成功"); } } //查看学生录入结果(无序)录入结果 public static void lookStudent( ArrayList<Student> arry1,ArrayList<Course> arry2) { if(arry1.size()==0) { System.out.println("请添加学生成绩,再查寻"); } for(int i=0;i<arry1.size();i++ ) { Student st=arry1.get(i); Course s=st.getA(); System.out.println("-------------------------------------------"); System.out.println("班级\t\t\t姓名\t\t学号\t\t计算\t英语\t数学\t政治"); System.out.println(st.getClassname()+"\t"+st.getName()+"\t\t"+st.getNumber()+"\t\t"+s.getComputer()+"\t"+s.getEnglish()+"\t"+s.getMath()+"\t"+s.getPolitics()); System.out.println("--------------------------------------------"); } } //对学生成绩排序,并且输出最高成绩和最低成绩 public static void sortStudent(ArrayList<Student> arry1,ArrayList<StudentList> arry3) { //对学生总成绩进行排序 //多少个学生排序 for(int i=0;i<arry1.size();i++) { StudentList a = new StudentList(); int sum=arry1.get(i).getA().getMath()+ arry1.get(i).getA().getEnglish()+ arry1.get(i).getA().getPolitics()+ arry1.get(i).getA().getComputer(); String name=arry1.get(i).getName(); a.setName(name); a.setSum(sum); arry3.add(a); } int j = 0; while( j<100) { int index=0; int i=0; for ( i = 0; i < arry3.size(); i++) { int max = arry3.get(0).getSum(); if (max < arry3.get(i).getSum()) { max = arry3.get(i).getSum(); index=i; } } if(j==0) { System.out.println("姓名\t\t总成绩最高分"); System.out.println(arry3.get(index).getName()+"\t\t"+arry3.get(index).getSum()); }else if((i==arry3.size())&&(i-1)==0) { System.out.println("姓名\t\t总成绩最低分"); System.out.println(arry3.get(index).getName()+"\t\t"+arry3.get(index).getSum()); }else { System.out.println("姓名\t\t总成绩"); System.out.println(arry3.get(index).getName() + "\t\t" + arry3.get(index).getSum()); } j++; arry3.remove(index); if(arry3.size()==0) { break; } } } //按学号、姓名、或课程名查询成绩 public static void searchStudent(ArrayList<Student> arry1,ArrayList<Course> arry2) { Scanner sc = new Scanner(System.in); System.out.println("按学号、姓名、或课程名查询成绩,学号查询请按1,姓名查询请按2"); int a = sc.nextInt(); if (a == 1) { System.out.println("请输入学号"); Scanner s = new Scanner(System.in); String number = s.nextLine(); for (int i = 0; i < arry1.size(); i++) { if (arry1.get(i).getNumber().equals(number)) { System.out.println("-------------------------------------------"); System.out.println("班级\t\t\t姓名\t\t学号\t\t计算\t英语\t数学\t政治"); System.out.println(arry1.get(i).getClassname() + "\t" + arry1.get(i).getName() + "\t\t" + arry1.get(i).getNumber() + "\t\t" + arry2.get(i).getComputer()+ "\t" + arry2.get(i).getEnglish() + "\t" +arry2.get(i).getMath() + "\t" + arry2.get(i).getPolitics()); System.out.println("--------------------------------------------"); } } } else if (a == 2) { System.out.println("请输入姓名"); Scanner c = new Scanner(System.in); String name = c.nextLine(); for (int i = 0; i < arry1.size(); i++) { if (arry1.get(i).getName().equals(name)) { System.out.println("-------------------------------------------"); System.out.println("班级\t\t\t姓名\t\t学号\t\t计算\t英语\t数学\t政治"); System.out.println(arry1.get(i).getClassname() + "\t" + arry1.get(i).getName() + "\t\t" + arry1.get(i).getNumber() + "\t\t" + arry1.get(i).getComputer() + "\t" + arry1.get(i).getEnglish() + "\t" + arry1.get(i).getMath() + "\t" + arry1.get(i).getPolitics()); System.out.println("--------------------------------------------"); } } } } //查看平均分与及格率 public static void averagStudent(ArrayList<Student> arry1,ArrayList<Course> arry2) { for(int i=0;i<arry1.size();i++) { int flag=0; double l=0; double averag=(arry2.get(i).getComputer()+arry2.get(i).getPolitics()+arry2.get(i).getEnglish()+arry2.get(i).getMath())*1.0/4; if(arry2.get(i).getComputer()<60) { flag++; } if (arry2.get(i).getPolitics()<60) { flag++; } if(arry2.get(i).getEnglish()<60) { flag++; } if(arry2.get(i).getMath()<60) { flag++; } l=(4-flag)/4.0; System.out.println("学号\t学生\t平均成绩\t及格率"); System.out.println(arry1.get(i).getNumber()+"\t"+arry1.get(i).getName()+"\t"+averag+"\t"+l); } } }
.