对我来说我学了3个月的Java觉的代码是能简验水平的工具其实我学了面向对象后就在试着写管理系统当然以我的水平写不出图形化界面不过其实在现在AI盛行的年代写个啥管理系统轻而易举的但我是学生是大一刚接触Java才三个月的学生我们的系主任每次看到我们用AI写代码就气不打一出来说AI写的永远不是你的介于主流编译器Java的一般是Idea因为我用过就知道它的AI提示功能太强大了写完了代码感觉1/3都是Tab键生成的所以我现在新学了什么我都去eclipse或VS code上写属于全手写一点提示都没有我就这样说300行的面向对象代码自己手写1个小时eclipseAi提示12分钟idea我试过快了好多虽然我的同学都说Java那么多方法谁去记那么多不如Ai提示可能是想法不同吧我还是第一次写一定全手写下面是我一个月前和现在写的小型学生成绩管理系统全是我手写的记录一下我的进步吧import java.util.*; public class Main{ public static class Person{//创建一个Person类 private String name; private int age; public Person(String name,int age) { this.namename; this.ageage; } //面向对象经典的set/get方法 public String getName() { return name; } public int getAge() { return age; } public void setName(String name) { if(namenull||name.isEmpty()) { System.out.println(姓名无效); }else { this.namename; } } public void setAge(int age) { if(age0||age120) { System.out.println(年龄无效); }else { this.ageage; } } //重写了toString方法其实不重写就是默认继承Object类中的toString方法,那样会输出地址。 Override public String toString() { return 姓名: name年龄: age; } } public static class Student extends Person{//创建Student类继承Person类 private int score; public Student(String name,int age,int score) { super(name,age); this.scorescore; } public int getScore() { return score; } public void setScore(int score) { if(score0||score100) { System.out.println(成绩无效); }else { this.scorescore; } } public void print() {//自定义print方法 System.out.println(学生的姓名是: getName()); System.out.println(学生的年龄是: getAge()); System.out.println(学生的成绩是: score); } } public static void main(String[] args) { Scanner inputnew Scanner(System.in); System.out.println(请输入学生人数: ); int ninput.nextInt(); ArrayList Studentlistnew ArrayList();//创建一个ArrayList集合,来实现增删改查。 input.nextLine(); for(int i0;in;i) { System.out.println(\n请输入第(i1)名同学的信息); System.out.println(请输入学生的姓名: ); String nameinput.nextLine(); System.out.println(请输入学生的年龄: ); int ageinput.nextInt(); System.out.println(请输入学生的成绩: ); int scoreinput.nextInt(); input.nextLine(); Student stunew Student(name,age,score); list.add(stu); } while(true) { System.out.println(欢迎使用学生成绩管理系统);//系统菜单 System.out.println(请输入您的选择: 1:打印学生信息 2:对学生成绩进行排序 3:增加学生信息 4:删减学生信息 5:退出程序); int choiceinput.nextInt(); input.nextLine(); switch(choice) { case 1: System.out.println(\n学生的信息表); for(Student s:list) { s.print(); System.out.println(); } break; case 2: Collections.sort(list,new ComparatorStudent(){//底层是双轴快速排序其实也可以写成Lambda表达式或方法引用 Override public int compare(Student o1,Student o2) { int score1o1.getScore(); int score2o2.getScore(); char name1o1.getName().charAt(0); char name2o2.getName().charAt(0); if(score1!score2) { return Integer.compare(score2,score1);//成绩降序 }else { return Character.compare(name1, name2);//姓名首字母升序 } } }); System.out.println(排序完成后的结果是: ); for(Student s:list){ s.print(); } break; case 3: System.out.println(\n增加学生); System.out.println(请添加学生姓名: ); String nameinput.nextLine(); System.out.println(请添加学生的年龄: ); int ageinput.nextInt(); System.out.println(请添加学生的成绩: ); int scoreinput.nextInt(); Student snew Student(name,age,score); list.add(s); break; case 4: System.out.println(请输入需要删除的学生的姓名: ); String DeleteNameinput.nextLine(); boolean foundfalse;//创建一个boolean变量用于判断是否找到该学生 for(int i0;ilist.size();i) { if(list.get(i).getName().equals(DeleteName)) { list.remove(i); foundtrue; break; } } if(!found) { System.out.println(未找到该学生,请重新输入!); } break; case 5: System.out.println(程序已退出欢迎下次使用); input.close(); return; default: System.out.println(请重新输入您的选择); break; } } } } 这个代码功能简单效率低ArrayList增删改查都O(n)了这是手写的第一个小作品是一个月前写的其实我现在学了IO,自定义异常就觉得HashMap来增删改查更快其实是我把前面这个代码给了豆包看了他说的所以我学了一个月就来实现豆包所说还是自己手写的只不过豆包给了我大方向的思路 1.自定义接口Studypublic interface Study {//接口的抽象方法是要重写的 abstract void study(); abstract void run(); abstract void write(); }2.自定义异常public class AgeOutOfBoundsException extends RuntimeException{ public AgeOutofBoundsException(){ }//空参构造 public AgeOutofBoundsException(String message){//带参构造 super(message); } }public class NameFormatException extends RuntimeException { public NameFormatException(){ } public NameFormatException(String message){ super(message); } }public class ScoreOutOfBoundsException extends RuntimeException{ public ScoreOutofBoundsException(){ } public ScoreOutofBoundsException(String message){ super(message); } }Student类public class Student implements Study { private String name; private int age; private double score; public Student() { } public Student(String name, int age, double score) {//这是加了简验功能 try { setName(name); setAge(age); setScore(score); } catch (Exception e) { System.err.println(学生对象创建失败 e.getMessage()); e.printStackTrace(); throw new RuntimeException(学生信息无效无法创建对象 e); } } public String getName() { return name; } public int getAge() { return age; } public double getScore() { return score; } public void setName(String name) { if (name null || name.isEmpty()) { throw new NameFormatException(姓名不能为空);//如果不满足就抛异常 } this.name name; } public void setAge(int age) { if (age 0 || age 150) { throw new AgeOutOfBoundsException(年龄必需在0-150之间); } this.age age; } public void setScore(double score) { if (score 0 || score 100.0) { throw new ScoreOutOfBoundsException(成绩必须在0-100之间); } this.score score; } public void setCategoryScore(double score) { if (score 90) { System.out.print(优秀); } else if (score 80 score 90) { System.out.print(良好); } else if (score 70 score 80) { System.out.print(中等); } else if (score 60 score 70) { System.out.print(及格); } else { System.out.print(不及格); } } public boolean isPass() { return score 60; } Override//重写的接口方法 public void study() { System.out.println(学生 name 在学习); } Override public void run() { System.out.println(学生 name 在奔跑); } Override public void write() { System.out.println(学生 name 在写作); } public void Show() { System.out.println(学生的姓名是 name); System.out.println(学生的年龄是 age); System.out.println(学生的得分为 score); System.out.print(学生成绩的情况为); setCategoryScore(score); System.out.println(isPass() ? 成绩及格 : 成绩不及格);//三目运算符判断 System.out.println(学生的日常生活为); study(); run(); write(); } }主测试类import java.io.*;//头文件不能少的 import java.util.*; public class Main{ public static void main(String[] args)throws IOException { BufferedReader brnew BufferedReader(new InputStreamReader(System.in)); PrintWriter outnew PrintWriter(System.out,true); File fnew File(D:\\Study\\学习.txt);//先开一个文件 try { if(!f.exists()){//如果不存在建建一个但要保证上一级的Study文件是存在的才行 f.createNewFile(); out.println(文件创建成功); }else{ out.println(文件已存在); } } catch (IOException e) { out.println(文件创建失败e.getMessage()); e.printStackTrace(); } out.println(请输入学生的个数: ); int nInteger.parseInt(br.readLine().trim()); HashMapString,Student mapnew HashMap();用哈希表来存可以天然有去重功能 Student []stunew Student[n];//学生类数组 for (int i 0; i n; i) { out.println(请输入第(i1)个学生的信息); out.println(请输入学生的姓名: ); String namebr.readLine().trim(); out.flush();//这个代码里面我了很多的flush为了更新资源其实我只是保险一点 out.println(请输入学生的学号: ); String idbr.readLine().trim(); out.flush(); out.println(请输入学生的年龄: ); int ageInteger.parseInt(br.readLine().trim()); out.flush(); out.println(请输入学生的成绩: ); double scoreDouble.parseDouble(br.readLine().trim()); out.flush(); try { stu[i]new Student(name,age,score); map.put(id,stu[i]);//学号作key,学生类做value所以前面的学生了我就没声明id变量了 }catch (Exception e){ out.println(输入错误e.getMessage()); e.printStackTrace(); i--; } } while (true) { out.println(欢迎使用学生成绩管理系统); out.println(输入1查看学生基本信息); out.println(输入2对学生成绩进行排序); out.println(输入3增加学生对象); out.println(输入4删除学生对象); out.println(输入5查询学生信息); out.println(输入6修改学生成绩); out.println(输入7保存学生信息); out.println(输入8退出系统); out.flush(); int choice Integer.parseInt(br.readLine().trim()); switch (choice) { case 1: if(map.isEmpty()){//只有不是空你才输出学生信息嘛 out.println(暂无学生信息请输入学生对象!); break; }else{ out.println(所有学生信息入下); map.entrySet().stream().forEach(e-{Lambada表达式 String ide.getKey(); Student se.getValue(); out.println(学号id ); s.Show(); out.flush(); out.println(-----------------------------); }); } out.flush(); break; case 2: if(map.isEmpty()){ out.println(暂无学生信息请输入学生对象在排序); return; }else{ PriorityQueueStudent pqnew PriorityQueue( (s1,s2)-Double.compare(s2.getScore(),s1.getScore()) );//优先队列排序默认的小根堆所以是降序 pq.addAll(map.values()); out.println(排序的结果是); int rank1; while(!pq.isEmpty()){ Student spq.poll(); out.println(第rank名的同学的信息是 ); s.Show(); out.flush(); rank; } } out.flush(); break; case 3: out.println(请输入需要添加的学生学号: ); String id1br.readLine().trim(); if(map.containsKey(id1)){ out.println(用户已存在请重新输入); break; }else{ out.println(请输入需要添加的学生的姓名: ); String name1br.readLine().trim(); out.println(请输入需要添加的学生的年龄: ); int age1Integer.parseInt(br.readLine().trim()); out.println(请输入需要添加的学生的成绩: ); double score1Double.parseDouble(br.readLine().trim()); try { map.put(id1,new Student(name1,age1,score1)); out.println(添加成功); }catch (Exception e){ out.println(添加失败e.getMessage()); } } out.flush(); break; case 4: if(map.isEmpty()){//删除的时候也要判空才行有学生对象你才删除嘛 out.println(暂无学生信息请添加后再删除); }else{ out.println(请输入需要删除的学生学号: ); String id3br.readLine().trim(); if(!map.containsKey(id3)){ out.println(该学生对象不存在请重新输入); }else{ if(map.remove(id3)!null){ out.println(删除成功); }else{ out.println(删除失败); } } } out.flush(); break; case 5: if(map.isEmpty()){ out.println(暂无学生对象请添加后再查询); }else{ out.println(请输入需要查询的学生的学号); String id4br.readLine().trim(); if(!map.containsKey(id4)){ out.println(没有该学生的信息请重新输入); }else{ out.println(该学生的信息如下: ); Student s1map.get(id4); s1.Show(); out.flush(); } } out.flush(); break; case 6: if(map.isEmpty()){//修改也是一样 out.println(暂无学生信息请添加学生对象后再修改); }else{ out.println(请输入需要修改的学生的对象的学号: ); String id5br.readLine().trim(); if(!map.containsKey(id5)){ out.println(没有该学生的信息请重新输入); }else{ map.get(id5).setScore(Double.parseDouble(br.readLine().trim())); out.println(修改成功); } } break; case 7: try(BufferedWriter bwnew BufferedWriter(new FileWriter(f,true))){//将信息存入文件中加了true其实就是每次运行不会把前一次写入的覆盖掉 bw.write(学生的基本信息如下); bw.newLine();/ SetString keysmap.keySet(); for(String key:keys){ Student s2map.get(key); bw.write(学生的学号是: key ); bw.newLine(); bw.write(学生的姓名是: s2.getName()); bw.newLine(); bw.write(学生的年龄是: s2.getAge()); bw.newLine(); bw.write(学生的得分是: s2.getScore()); bw.write(--------------------------------------); bw.newLine(); bw.flush(); } out.println(写入成功文件路径为f.getAbsolutePath()); }catch(IOException e){ out.println(写入失败e.getMessage()); e.printStackTrace(); } out.flush(); break; case 8: out.println(已退出系统欢迎下次使用); out.flush(); return; default: out.println(输入不合要求请重新输入); out.flush(); break; } out.flush(); } } }在这个代码中用了PriorityQueue排序排序速度其实还不如Collections.sort但我正好学了所以就写进去了BufferedReader,PrintWriter输入输出都快的多后来我写出来给豆包看他说比上一版效率了6倍其实我考虑到BufferedReader输出好像会有空格所以trim了一下用了自定义异常就可真的校验输入内容了其实我可以加多成绩的还有很大改进空间加油等学完了多线程和Swing,我会进一步改进多手写多练习