Java学生管理系统(基于C语言学生管理系统),简单、易懂、链表实现。

    xiaoxiao2026-03-07  3

    我写的这个代码几乎和C语言的模式一模一样,很好理解。 我的代码对学生类对象、课程类对象、成绩类对象初始化的时候全部初始化为null,在之后的操作中并没有对他们的"头"进行操作直接操作了"头->next",所以每个对象链表的第一个域全是null。

    我的这个能实现的功能:

    1.显示所有学生 2.学生添加 3.学生删除 4.学生信息重新登记 5.根据学号查找学生 6.根据姓名查找学生 1.显示所有课程 2.课程添加 3.课程删除 4.课程名修改 1.成绩登记 2.成绩修改 3.成绩查询 4.成绩排序 5.成绩打印(生成一个txt文档 每条成绩占一行) 6.列出某门课所有学生成绩

    以下是大致的结构

    学生类课程类成绩类方法类的接口方法类的实现主函数

    1.学生类

    package studentManager; public class StudentInformation { String studentName = null; String studentID = null; String studentsex = null; int studentAge = 0; double sumGrade = 0; //总成绩(后面排序用到) StudentInformation next = null; StudentInformation() { this.studentName = null; this.studentID = null; this.studentsex = null; this.studentAge = 0; this.sumGrade = 0; this.next = null; } }

    2.课程类

    package studentManager; public class CourseInformation { String courseName; CourseInformation next; CourseInformation() { this.courseName = null; this.next = null; } }

    3.成绩类

    package studentManager; public class GradeInformation { double courseGrade = 0; //该学生该课程的分数 String gradeStudentName; //学生姓名 String gradeCourseName; //课程名称 GradeInformation next; GradeInformation() { this.courseGrade = 0; gradeStudentName = null; gradeCourseName = null; } }

    4.方法的接口

    package studentManager; import java.io.IOException; public interface MethodInterface { //学生信息管理 void showAllStudents(StudentInformation p); //1.显示所有学生 void addStudent(StudentInformation p);//2.学生添加 void deleteStudent(StudentInformation p);//3.学生删除 void registerStudent(StudentInformation p);//4.学生信息重新登记 void searchStudentInID(StudentInformation p);//5.根据学号查找学生 void searchStudentInNmae(StudentInformation p);//6.根据姓名查找学生 //课程信息管理 void showAllCourses(CourseInformation p);//1.显示所有课程 void addCourse(CourseInformation p);//2.课程添加 void deleteCourse(CourseInformation p);//3.课程删除 void renameCourse(CourseInformation p);//4.课程名修改 //成绩信息管理 //1.成绩登记 void registerGrade(CourseInformation courseP, StudentInformation studentP, GradeInformation gradeP); //2.成绩修改 void changeGrade(CourseInformation courseP, StudentInformation studentP, GradeInformation gradeP); //3.成绩查询 void searchCourseGrade(CourseInformation courseP, StudentInformation studentP, GradeInformation gradeP); //4.成绩排序 void sortGrades(CourseInformation courseP, StudentInformation studentP, GradeInformation gradeP); //5.成绩打印(生成一个txt文档 每条成绩占一行) void printInFile(CourseInformation courseP, StudentInformation studentP, GradeInformation gradeP) throws IOException; //6.列出某门课所有学生成绩 void showOneCourseAllStudentGrades(CourseInformation courseP, StudentInformation studentP, GradeInformation gradeP); }

    5.方法的实现(这个一共596行代码,就把他放到了最后↓↓↓↓↓↓)

    6.主函数

    package studentManager; import java.io.IOException; import java.util.Scanner; import com.sun.tools.classfile.StackMap_attribute.stack_map_frame; public class MainController { static StudentInformation student = new StudentInformation(); static CourseInformation course = new CourseInformation(); static GradeInformation grade = new GradeInformation(); static Method method = new Method(); public static void main(String[] args) throws IOException { int code; Scanner input = new Scanner(System.in); while (true) { mainMenu(); System.out.println("请输入操作码"); code = input.nextInt(); switch (code) { case 1: studentMenu(); break; case 2: courseMenu(); break; case 3: gradeMenu(); break; case 0: input.close(); System.exit(0); } } } static void mainMenu() { System.out.println("1.学生信息管理"); System.out.println("2.课程信息管理"); System.out.println("3.成绩信息管理"); System.out.println("0.退出系统"); } static void studentMenu() { System.out.println("1.显示所有学生\r\n" + "2.学生添加\r\n" + "3.学生删除\r\n" + "4.学生信息重新登记\r\n" + "5.根据学号查找学生\r\n" + "6.根据姓名查找学生\r\n" + "0.返回\r\n" + ""); int code; Scanner input = new Scanner(System.in); while (true) { int flag = 0; System.out.println("请输入操作码(学生信息管理)"); code = input.nextInt(); switch (code) { case 1: method.showAllStudents(student); break; case 2: method.addStudent(student); break; case 3: method.deleteStudent(student) ; break; case 4: method.registerStudent(student); break; case 5: method.searchStudentInID(student); break; case 6: method.searchStudentInNmae(student); break; case 0: flag = 1; break; } if(flag == 1) { break; } suspend(); System.out.println("1.显示所有学生\r\n" + "2.学生添加\r\n" + "3.学生删除\r\n" + "4.学生信息重新登记\r\n" + "5.根据学号查找学生\r\n" + "6.根据姓名查找学生\r\n" + "0.返回\r\n" + ""); } } static void gradeMenu() throws IOException { System.out.println("1.成绩登记\r\n" + "2.成绩修改\r\n" + "3.成绩查询\r\n" + "4.成绩排序(按分数排序 按学号排序)\r\n" + "5.成绩打印(生成一个txt文档 每条成绩占一行)\r\n" + "6.列出某门课所有学生成绩\r\n" + "0.返回\r\n" + ""); int code; Scanner input = new Scanner(System.in); while (true) { int flag = 0; System.out.println("请输入操作码(成绩管理)"); code = input.nextInt(); switch (code) { case 1: method.registerGrade(course, student, grade); break; case 2: method.changeGrade(course, student, grade); break; case 3: method.searchCourseGrade(course, student, grade); break; case 4: method.sortGrades(course, student, grade); break; case 5: method.printInFile(course, student, grade); break; case 6: method.showOneCourseAllStudentGrades(course, student, grade); break; case 0: flag = 1; break; } if(flag == 1) { break; } suspend(); System.out.println("1.成绩登记\r\n" + "2.成绩修改\r\n" + "3.成绩查询\r\n" + "4.成绩排序(按分数排序 按学号排序)\r\n" + "5.成绩打印(生成一个txt文档 每条成绩占一行)\r\n" + "6.列出某门课所有学生成绩\r\n" + "0.返回\r\n" + ""); } } static void courseMenu() { System.out.println("1.显示所有课程\r\n" + "2.课程添加\r\n" + "3.课程删除\r\n" + "4.课程名修改\r\n" + "0.返回\r\n" + ""); int code; Scanner input = new Scanner(System.in); while (true) { int flag = 0; System.out.println("请输入操作码(课程管理)"); code = input.nextInt(); switch (code) { case 1: method.showAllCourses(course); break; case 2: method.addCourse(course); break; case 3: method.deleteCourse(course); break; case 4: method.renameCourse(course); break; case 0: flag = 1; break; } if(flag == 1) { break; } suspend(); System.out.println("1.显示所有课程\r\n" + "2.课程添加\r\n" + "3.课程删除\r\n" + "4.课程名修改\r\n" + "0.返回\r\n" + ""); } } //为了实现和类似于C语言中的system("pause");的功能 static void suspend() { Scanner tmp = new Scanner(System.in); System.out.println("请输入'c'(任意键)+enter继续......"); tmp.hasNext(); } }

    运行之后大致是这样子的

    只需要跟着程序的提示一步步操作就可以了 5.最后附上方法类的实现

    package studentManager; import java.io.IOException; import java.util.Scanner; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.OutputStream; public class Method implements MethodInterface { @Override public void showAllStudents(StudentInformation p) { // TODO Auto-generated method stub StudentInformation tmp = p; if (tmp == null) { System.out.println("======华丽分割线======"); return; } System.out.println("姓名\t学号\t性别\t年龄\t总成绩"); while (tmp != null) { System.out.println(tmp.studentName + "\t" + tmp.studentID + "\t" + tmp.studentsex + "\t" + tmp.studentAge + "\t" + tmp.sumGrade); tmp = tmp.next; } } @Override public void addStudent(StudentInformation p) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); StudentInformation head = p; StudentInformation p1 = new StudentInformation(); String name, id, sex; int age; while (true) { System.out.println("请输入学生的姓名,学号,性别,年龄(全是0结束输入)"); name = input.next(); id = input.next(); sex = input.next(); age = input.nextInt(); if (age == 0) { break; } p1.studentName = name; p1.studentID = id; p1.studentsex = sex; p1.studentAge = age; if (head == null) { head = p1; } else { StudentInformation tmp = head; while (tmp.next != null) { tmp = tmp.next; } tmp.next = p1; p1 = new StudentInformation(); // p1重新更新,要不然next指针域会一直等于上一个的指针域 } } } @Override public void deleteStudent(StudentInformation p) { // TODO Auto-generated method stub StudentInformation head = p; Scanner input = new Scanner(System.in); String name; System.out.println("请输入要删除学生的姓名"); name = input.next(); StudentInformation p1 = new StudentInformation(); StudentInformation p2 = new StudentInformation(); p1 = head; if (head == null) { System.out.println("没有该学生"); return; } for (; p1 != null; p2 = p1, p1 = p1.next) { if (p1.studentName == null) // 没有这个if会空指针异常报错 continue; if (p1.studentName.equals(name) && p1 == head) { head = p1.next; System.out.println("1.已删除 " + name); return; } else if (p1.studentName.equals(name)) { p2.next = p1.next; System.out.println("2.已删除 " + name); return; } if (p1.next == null) { System.out.println("没有该学生"); return; } } p = head; } @Override public void registerStudent(StudentInformation p) { // TODO Auto-generated method stub StudentInformation tmp = p; Scanner input = new Scanner(System.in); String name = new String(); System.out.println("请输入要重新注册学生的姓名"); name = input.next(); if (tmp == null) { System.out.println("没有该学生"); return; } while (tmp != null) { if (tmp.studentName == null) { // 没有这个if会空指针异常报错 tmp = tmp.next; continue; } if (tmp.studentName.equals(name)) { String tmpname, id, sex; int age; System.out.println("(重新注册)请输入学生的姓名,学号,性别,年龄"); tmpname = input.next(); id = input.next(); sex = input.next(); age = input.nextInt(); System.out.println(tmp.studentName); tmp.studentName = tmpname; tmp.studentID = id; tmp.studentsex = sex; tmp.studentAge = age; System.out.println("修改成功"); System.out.println(" 修改为 " + name + ", " + id + ", " + sex + "," + age); return; } tmp = tmp.next; } System.out.println("没有该学生"); } @Override public void searchStudentInID(StudentInformation p) { // TODO Auto-generated method stub StudentInformation tmp = p; Scanner input = new Scanner(System.in); String id; System.out.println("请输入要查找学生的学号"); id = input.next(); if (tmp == null) { System.out.println("没有该学生"); return; } while (tmp != null) { if (tmp.studentID == null) { // 没有这个if会空指针异常报错 tmp = tmp.next; continue; } if (tmp.studentID.equals(id)) { System.out.println("姓名\t学号\t性别\t成绩"); System.out.println( tmp.studentName + "\t" + tmp.studentID + "\t" + tmp.studentsex + "\t" + tmp.studentAge); return; } tmp = tmp.next; } System.out.println("没有该学生"); } @Override public void searchStudentInNmae(StudentInformation p) { // TODO Auto-generated method stub StudentInformation tmp = p; Scanner input = new Scanner(System.in); String name; System.out.println("请输入要查找学生的姓名"); name = input.next(); if (tmp == null) { System.out.println("没有该学生"); return; } while (tmp != null) { if (tmp.studentName == null) { // 没有这个if会空指针异常报错 tmp = tmp.next; continue; } if (tmp.studentName.equals(name)) { System.out.println("姓名\t学号\t性别\t成绩"); System.out.println( tmp.studentName + "\t" + tmp.studentID + "\t" + tmp.studentsex + "\t" + tmp.studentAge); return; } tmp = tmp.next; } System.out.println("没有该学生"); } @Override public void showAllCourses(CourseInformation p) { // TODO Auto-generated method stub CourseInformation tmp = p; if (tmp == null) { tmp = tmp.next; } if (tmp == null) { return; } System.out.println("所有课程:"); while (tmp != null) { System.out.println(tmp.courseName); tmp = tmp.next; } } @SuppressWarnings("unlikely-arg-type") @Override public void addCourse(CourseInformation p) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); CourseInformation head = p; CourseInformation p1 = new CourseInformation(); String name, id, sex; int age; while (true) { System.out.println("请输入课程的名称(0结束输入)"); name = input.next(); if (name.equals("0")) { break; } p1.courseName = name; if (head == null) { head = p1; } else { CourseInformation tmp = head; while (tmp.next != null) { tmp = tmp.next; } tmp.next = p1; p1 = new CourseInformation(); // p1重新更新,要不然next指针域会一直等于上一个的指针域 } } } @Override public void deleteCourse(CourseInformation p) { // TODO Auto-generated method stub CourseInformation head = p; Scanner input = new Scanner(System.in); String name; System.out.println("请输入要删除的课程"); name = input.next(); CourseInformation p1 = new CourseInformation(); CourseInformation p2 = new CourseInformation(); p1 = head; if (head == null) { System.out.println("没有该课程"); return; } for (; p1 != null; p2 = p1, p1 = p1.next) { if (p1.courseName == null) // 没有这个if会空指针异常报错 continue; if (p1.courseName.equals(name) && p1 == head) { head = p1.next; System.out.println("1.已删除 " + name); return; } else if (p1.courseName.equals(name)) { p2.next = p1.next; System.out.println("2.已删除 " + name); return; } if (p1.next == null) { System.out.println("没有该课程"); return; } } p = head; } @Override public void renameCourse(CourseInformation p) { // TODO Auto-generated method stub CourseInformation tmp = p; Scanner input = new Scanner(System.in); String name = new String(); System.out.println("请输入要重新注册的课程"); name = input.next(); if (tmp == null) { System.out.println("没有该课程"); return; } while (tmp != null) { if (tmp.courseName == null) { // 没有这个if会空指针异常报错 tmp = tmp.next; continue; } if (tmp.courseName.equals(name)) { String tmpname; System.out.println("(重新注册)请输入课程的名称"); tmpname = input.next(); tmp.courseName = tmpname; System.out.println("修改成功"); System.out.println(" 修改为 " + tmpname); return; } tmp = tmp.next; } System.out.println("没有该课程"); } @Override public void registerGrade(CourseInformation courseP, StudentInformation studentP, GradeInformation gradeP) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("请输入要登记的学生的姓名"); String studentname = new String(); studentname = input.next(); StudentInformation tmpstudent = studentP; if (tmpstudent == null) { System.out.println("没有该学生"); return; } while (tmpstudent != null) { if (tmpstudent.studentName == null) { tmpstudent = tmpstudent.next; continue; } if (tmpstudent.studentName.equals(studentname)) { CourseInformation tmpcourse = courseP; System.out.println("请输入登记的课程"); String coursename = new String(); coursename = input.next(); if (tmpcourse == null) { System.out.println("没有该课程"); return; } while (tmpcourse != null) { if (tmpcourse.courseName == null) { tmpcourse = tmpcourse.next; continue; } if (tmpcourse.courseName.equals(coursename)) { System.out.println("给" + studentname + "的" + coursename + "打分"); double grade = 0; grade = input.nextDouble(); GradeInformation tmpgrade = gradeP; if (tmpgrade == null) { tmpgrade.gradeStudentName = studentname; tmpgrade.gradeCourseName = coursename; tmpgrade.courseGrade = grade; tmpstudent.sumGrade += grade; return; } while (tmpgrade.next != null) { tmpgrade = tmpgrade.next; } GradeInformation kidgrade = new GradeInformation(); kidgrade.gradeStudentName = studentname; kidgrade.gradeCourseName = coursename; kidgrade.courseGrade = grade; tmpstudent.sumGrade += grade; kidgrade.next = null; tmpgrade.next = kidgrade; return; } tmpcourse = tmpcourse.next; } System.out.println("没有该课程"); } tmpstudent = tmpstudent.next; } } @Override public void changeGrade(CourseInformation courseP, StudentInformation studentP, GradeInformation gradeP) { // TODO Auto-generated method stub System.out.println("请输入修改的学生姓名"); Scanner input = new Scanner(System.in); String studentname = new String(); studentname = input.next(); StudentInformation tmpstudent = studentP; if (tmpstudent == null) { System.out.println("没有该学生"); return; } while (tmpstudent != null) { if (tmpstudent.studentName == null) { tmpstudent = tmpstudent.next; continue; } if (tmpstudent.studentName.equals(studentname)) { CourseInformation tmpcourse = courseP; System.out.println("请输入修改要成绩的课程"); String coursename = new String(); coursename = input.next(); if (tmpcourse == null) { System.out.println("没有该课程"); return; } while (tmpcourse != null) { if (tmpcourse.courseName == null) { tmpcourse = tmpcourse.next; continue; } if (tmpcourse.courseName.equals(coursename)) { GradeInformation tmpgrade = gradeP; while (tmpgrade != null) { if (tmpgrade.gradeCourseName == null || tmpgrade.gradeStudentName == null) { tmpgrade = tmpgrade.next; continue; } if (tmpgrade.gradeCourseName.equals(coursename) && tmpgrade.gradeStudentName.equals(studentname)) { System.out.println("请打分(修改)"); tmpstudent.sumGrade -= tmpgrade.courseGrade; double grade = 0; grade = input.nextDouble(); tmpgrade.courseGrade = grade; tmpstudent.sumGrade += grade; return; } tmpgrade = tmpgrade.next; } } tmpcourse = tmpcourse.next; } System.out.println("没有该课程"); return; } tmpstudent = tmpstudent.next; } System.out.println("没有该学生"); } @Override public void searchCourseGrade(CourseInformation courseP, StudentInformation studentP, GradeInformation gradeP) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("请输入查找学生的姓名, 课程的名称"); String coursename = new String(); String studentname = new String(); studentname = input.next(); coursename = input.next(); GradeInformation tmpgrade = gradeP; if (tmpgrade == null) { System.out.println("没有学生或者该课程"); return; } while (tmpgrade != null) { if (tmpgrade.gradeStudentName == null || tmpgrade.gradeCourseName == null) { tmpgrade = tmpgrade.next; continue; } if (tmpgrade.gradeStudentName.equals(studentname) && tmpgrade.gradeCourseName.equals(coursename)) { System.out.println(studentname + " 的 " + coursename + " 成绩是 " + tmpgrade.courseGrade); return; } tmpgrade = tmpgrade.next; } System.out.println("没有学生或者该课程"); } @Override public void sortGrades(CourseInformation courseP, StudentInformation studentP, GradeInformation gradeP) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); StudentInformation psort = new StudentInformation(); StudentInformation p = new StudentInformation(); StudentInformation pmax = new StudentInformation(); psort.next = studentP; if(psort.next == null) { System.out.println("排序完成(学生总成绩降序)"); return ; } while(psort.next != null) { pmax = psort.next; p = psort.next.next; while(p != null) { if(pmax.sumGrade < p.sumGrade) { pmax = p; } p = p.next; } String name = new String(); String id = new String(); String sex = new String(); int age = 0; double sum = 0; name = pmax.studentName; id = pmax.studentID; sex = pmax.studentsex; age = pmax.studentAge; sum = pmax.sumGrade; pmax.studentName = psort.next.studentName; pmax.studentID = psort.next.studentID; pmax.studentsex = psort.next.studentsex; pmax.studentAge = psort.next.studentAge; pmax.sumGrade = psort.next.sumGrade; psort.next.studentName = name; psort.next.studentID = id; psort.next.studentsex = sex; psort.next.studentAge = age; psort.next.sumGrade = sum; //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- psort = psort.next; } System.out.println("排序完成(学生总成绩降序)"); } @Override public void printInFile(CourseInformation courseP, StudentInformation studentP, GradeInformation gradeP) throws IOException { // TODO Auto-generated method stub FileWriter fout = new FileWriter("F:\\java\\testworksapace\\StudentManger03\\FileSave.txt", false); StudentInformation tmpstudent = studentP; String[] stuname = new String[1000000]; int num = 0; if(tmpstudent == null) { return ; } while(tmpstudent != null) { if(tmpstudent.studentName == null) { tmpstudent = tmpstudent.next; continue; } stuname[num ++] = tmpstudent.studentName; tmpstudent = tmpstudent.next; } CourseInformation tmpcourse = courseP; String[] coursename = new String[1000000]; int cnt = 0; while(tmpcourse != null) { if(tmpcourse.courseName== null) { tmpcourse = tmpcourse.next; continue; } coursename[cnt ++] = tmpcourse.courseName; tmpcourse = tmpcourse.next; } fout.write("姓名\t"); for(String i : coursename) { if(i == null) { continue; } fout.write(i + "\t"); } fout.write("\r\n"); int pos = 0; for(int k = 0; k < num; k ++) { String i = stuname[k]; if(i == null) { continue; } pos = 0; fout.write(i + "\t"); GradeInformation tmpgrade = gradeP; while(tmpgrade != null) { if(tmpgrade.gradeStudentName == null) { tmpgrade = tmpgrade.next; continue; } if(tmpgrade.gradeStudentName.equals(i)) { if(tmpgrade.gradeCourseName.equals(coursename[pos])) { fout.write((int)tmpgrade.courseGrade + "\t"); pos ++; } } tmpgrade = tmpgrade.next; } fout.write("\r\n"); } fout.close(); System.out.println("储存完成"); } @Override public void showOneCourseAllStudentGrades(CourseInformation courseP, StudentInformation studentP, GradeInformation gradeP) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("请输入学生的姓名"); String studentname = new String(); studentname = input.next(); GradeInformation tmpgrade = gradeP; if (tmpgrade == null) { System.out.println("没有该学生"); return; } int cnt = 0; while (tmpgrade != null) { if (tmpgrade.gradeStudentName == null) { tmpgrade = tmpgrade.next; continue; } if (tmpgrade.gradeStudentName.equals(studentname)) { cnt++; if (cnt == 1) { System.out.println("课程\t\t成绩"); } System.out.println(tmpgrade.gradeCourseName + "\t\t" + tmpgrade.courseGrade); } tmpgrade = tmpgrade.next; } } }
    最新回复(0)