本文共 6138 字,大约阅读时间需要 20 分钟。
控制类(主类):Manage
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.Scanner;public class Manage{ static ListstudentList=new ArrayList<>(); static List allscores=new ArrayList<>(); static Scanner in=new Scanner(System.in); public static void main(String[] args) { int ty = 10; while(ty!=0) { 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("8.成绩单"); System.out.println("0.退出系统"); ty=in.nextInt(); switch(ty) { case 1: addStudent(); break; case 2: remove(); break; case 3:fix(); break; case 4:find(); break; case 5:addscore(); break; case 6:sort(); break; case 7:printscore(); break; case 8:findall(); break; case 0:break; default:System.out.println("请重新输入"); } } System.out.println("系统退出成功!"); } public static void addStudent() { Student student=new Student(); System.out.println("请输入名字:"); student.getname(in.next()); System.out.println("请输入学号:"); student.getnum(in.nextInt()); studentList.add(student); studentList.get(studentList.size()-1).studentScores.add(new Score()); System.out.println("添加成功!"); } public static void addscore() { System.out.println("请输入要增加课程成绩记录学生学号:"); int p=in.nextInt(); int t=0; for (int i = 0; i < studentList.size(); i++) { if (p==studentList.get(i).tonum()) { studentList.get(i).studentScores.add(new Score()); System.out.println("添加课程成绩成功"); t=1; } } if (t==0) { System.out.println("没有找到学号为:"+p+"学生"); } } public static void remove() { System.out.println("请输入要删除学生的学号:"); int p=in.nextInt(); int t=0; for (int i = 0; i < studentList.size(); i++) { if (p==studentList.get(i).tonum()) { studentList.remove(i); t=1; } } if (t==0) { System.out.println("没有找到学号为:"+p+"学生"); } } public static void fix() { System.out.println("请输入要修改信息的学生学号:"); int p=in.nextInt(); int t=0; int t1=0; String scString; for (int i = 0; i < studentList.size(); i++) { if (p==studentList.get(i).tonum()) { System.out.println("请输入要修改课程(全称):"); scString=in.next(); for (int j = 0; j < studentList.get(i).studentScores.size(); j++) { if (scString.equals(studentList.get(i).studentScores.get(j).course)) { //scString==(studentList.get(i).studentScores.get(j).course恶意出错 System.out.println("请输入要修改此门课:"+scString+"的成绩:"); studentList.get(i).studentScores.get(j).score=in.nextInt(); t1=1; } } if (t1==0) { System.out.println("没有找到课程为:"+scString+"的课程"); } t=1; } } if (t==0) { System.out.println("没有找到学号为:"+p+"学生"); } } public static void find() { System.out.println("请输入要查找信息的学生学号:"); int p=in.nextInt(); int t=0; for (int i = 0; i < studentList.size(); i++) { if (p==studentList.get(i).tonum()) { System.out.println("该生信息如下:"); System.out.println("学号:"+studentList.get(i).tonum()); System.out.println("姓名:"+studentList.get(i).toname()); for (int j = 0; j < studentList.get(i).studentScores.size(); j++) { System.out.println("课程:"+studentList.get(i).studentScores.get(j).course); System.out.println("成绩:"+studentList.get(i).studentScores.get(j).score); } t=1; } } if (t==0) { System.out.println("没有找到学号为:"+p+"学生"); } } public static void sort() { for (int i = 0; i < studentList.size(); i++) { Collections.sort(studentList.get(i).studentScores,new Comparator () { @Override public int compare(Score o1, Score o2) { // TODO 自动生成的方法存根 return -o1.score+o2.score; } }); } findall(); } public static void findall() { for (int i = 0; i < studentList.size(); i++) { System.out.println("学号:"+studentList.get(i).num+" "+"姓名:"+studentList.get(i).name); for (int j = 0; j < studentList.get(i).studentScores.size(); j++) { System.out.println("课程:"+studentList.get(i).studentScores.get(j).course+" "+"成绩:"+studentList.get(i).studentScores.get(j).score); } } } public static void printscore() { System.out.println(allscores.size()); for (int i = 0; i < allscores.size(); i++) { System.out.println("课程:"+allscores.get(i).course+" "+"人数:"+allscores.get(i).pepnumsum+" "+"总分:"+allscores.get(i).scoresum+" "+"平均分:"+allscores.get(i).scoresum/allscores.get(i).pepnumsum+" "+"最高分:"+allscores.get(i).heightest+" "+"最低分:"+allscores.get(i).lowest); } }}```> 学生类:Student```javaimport java.util.ArrayList;import java.util.List;public class Student { public String name; public int num; public List studentScores=new ArrayList<>(); public void getname(String name) { this.name=name; } public void getnum(int num) { this.num=num; } public String toname() { return name; } public int tonum() { return num; }}
添加成绩记录类:Score
import java.util.Scanner;public class Score extends Manage{ public String course; public int score; Score() { Scanner scannerin=new Scanner(System.in); System.out.println("请输入要添加的课程:"); this.course=scannerin.next(); System.out.println("请输入此门课程的成绩:"); this.score=scannerin.nextInt(); Allscore h=new Allscore(this.course, this.score); if (h.p==1) { allscores.add(h); } } }
统计平均分,最高分,最低分类:Allscore
public class Allscore extends Manage{ public String course; public int scoresum; public int pepnumsum; public int heightest; public int lowest; public int p=1; public Allscore(String course, int score) { for (int i = 0; i < allscores.size(); i++) { if (course.equals(allscores.get(i).course)) { allscores.get(i).scoresum+=score; allscores.get(i).pepnumsum+=1; if (allscores.get(i).heightestscore) { allscores.get(i).lowest=score; } p=0; } } if (p==1) { this.course=course; this.scoresum=score; this.heightest=score; this.lowest=score; this.pepnumsum=1; } }}
之后我会实现一个界面版,敬请期待
转载地址:http://zorwi.baihongyu.com/