news 2026/5/1 14:32:24

最近Java学习的总结:

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
最近Java学习的总结:

对我来说,我学了3个月的Java,觉的代码是能简验水平的工具,其实我学了面向对象后,就在试着写管理系统,当然以我的水平,写不出图形化界面,不过其实在现在AI盛行的年代,写个啥管理系统轻而易举的,但我是学生,是大一刚接触Java才三个月的学生,我们的系主任每次看到我们用AI写代码就气不打一出来,说:AI写的永远不是你的,介于主流编译器(Java)的一般是Idea,因为我用过就知道,它的AI提示功能太强大了,写完了代码感觉1/3都是Tab键生成的,所以我现在新学了什么,我都去eclipse或VS code上写,属于全手写一点提示都没有,我就这样说,300行的面向对象代码,自己手写1个小时(eclipse),Ai提示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.name=name; this.age=age; } //面向对象经典的set/get方法 public String getName() { return name; } public int getAge() { return age; } public void setName(String name) { if(name==null||name.isEmpty()) { System.out.println("姓名无效!"); }else { this.name=name; } } public void setAge(int age) { if(age<0||age>120) { System.out.println("年龄无效!"); }else { this.age=age; } } //重写了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.score=score; } public int getScore() { return score; } public void setScore(int score) { if(score<0||score>100) { System.out.println("成绩无效!"); }else { this.score=score; } } public void print() {//自定义print方法 System.out.println("学生的姓名是: "+getName()); System.out.println("学生的年龄是: "+getAge()); System.out.println("学生的成绩是: "+score); } } public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("请输入学生人数: "); int n=input.nextInt(); ArrayList <Student>list=new ArrayList<>();//创建一个ArrayList集合,来实现增删改查。 input.nextLine(); for(int i=0;i<n;i++) { System.out.println("\n=====请输入第"+(i+1)+"名同学的信息"); System.out.println("请输入学生的姓名: "); String name=input.nextLine(); System.out.println("请输入学生的年龄: "); int age=input.nextInt(); System.out.println("请输入学生的成绩: "); int score=input.nextInt(); input.nextLine(); Student stu=new Student(name,age,score); list.add(stu); } while(true) { System.out.println("=======欢迎使用学生成绩管理系统=======");//系统菜单 System.out.println("请输入您的选择: 1:打印学生信息 2:对学生成绩进行排序 3:增加学生信息 4:删减学生信息 5:退出程序"); int choice=input.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 Comparator<Student>(){//底层是双轴快速排序,其实也可以写成Lambda表达式或方法引用 @Override public int compare(Student o1,Student o2) { int score1=o1.getScore(); int score2=o2.getScore(); char name1=o1.getName().charAt(0); char name2=o2.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 name=input.nextLine(); System.out.println("请添加学生的年龄: "); int age=input.nextInt(); System.out.println("请添加学生的成绩: "); int score=input.nextInt(); Student s=new Student(name,age,score); list.add(s); break; case 4: System.out.println("请输入需要删除的学生的姓名: "); String DeleteName=input.nextLine(); boolean found=false;//创建一个boolean变量,用于判断是否找到该学生 for(int i=0;i<list.size();i++) { if(list.get(i).getName().equals(DeleteName)) { list.remove(i); found=true; 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.自定义接口Study:
public 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 br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(System.out,true); File f=new 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 n=Integer.parseInt(br.readLine().trim()); HashMap<String,Student> map=new HashMap<>();用哈希表来存,可以天然有去重功能 Student []stu=new Student[n];//学生类数组 for (int i = 0; i < n; i++) { out.println("请输入第"+(i+1)+"个学生的信息!"); out.println("请输入学生的姓名: "); String name=br.readLine().trim(); out.flush();//这个代码里面我+了很多的flush为了更新资源,其实我只是保险一点 out.println("请输入学生的学号: "); String id=br.readLine().trim(); out.flush(); out.println("请输入学生的年龄: "); int age=Integer.parseInt(br.readLine().trim()); out.flush(); out.println("请输入学生的成绩: "); double score=Double.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 id=e.getKey(); Student s=e.getValue(); out.println("学号:"+id+" "); s.Show(); out.flush(); out.println("-----------------------------"); }); } out.flush(); break; case 2: if(map.isEmpty()){ out.println("暂无学生信息,请输入学生对象在排序!"); return; }else{ PriorityQueue<Student> pq=new PriorityQueue<>( (s1,s2)->Double.compare(s2.getScore(),s1.getScore()) );//优先队列排序,默认的小根堆,所以是降序 pq.addAll(map.values()); out.println("==============排序的结果是================"); int rank=1; while(!pq.isEmpty()){ Student s=pq.poll(); out.println("第"+rank+"名的同学的信息是"+" "); s.Show(); out.flush(); rank++; } } out.flush(); break; case 3: out.println("请输入需要添加的学生学号: "); String id1=br.readLine().trim(); if(map.containsKey(id1)){ out.println("用户已存在,请重新输入!"); break; }else{ out.println("请输入需要添加的学生的姓名: "); String name1=br.readLine().trim(); out.println("请输入需要添加的学生的年龄: "); int age1=Integer.parseInt(br.readLine().trim()); out.println("请输入需要添加的学生的成绩: "); double score1=Double.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 id3=br.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 id4=br.readLine().trim(); if(!map.containsKey(id4)){ out.println("没有该学生的信息,请重新输入!"); }else{ out.println("该学生的信息如下: "); Student s1=map.get(id4); s1.Show(); out.flush(); } } out.flush(); break; case 6: if(map.isEmpty()){//修改也是一样 out.println("暂无学生信息,请添加学生对象后再修改!"); }else{ out.println("请输入需要修改的学生的对象的学号: "); String id5=br.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 bw=new BufferedWriter(new FileWriter(f,true))){//将信息存入文件中加了true其实就是每次运行不会把前一次写入的覆盖掉 bw.write("==========学生的基本信息如下=========="); bw.newLine();/ Set<String> keys=map.keySet(); for(String key:keys){ Student s2=map.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,我会进一步改进!多手写,多练习!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/1 14:31:21

开发智能客服场景时,如何借助多模型能力提升回答质量与稳定性

开发智能客服场景时&#xff0c;如何借助多模型能力提升回答质量与稳定性 1. 智能客服系统的多模型接入需求 在构建智能客服系统时&#xff0c;单一模型往往难以覆盖所有用户问题的多样性。专业领域咨询需要模型具备垂直知识&#xff0c;而日常对话则更依赖语言理解能力。通过…

作者头像 李华
网站建设 2026/5/1 14:26:25

3分钟搞定微信QQ语音播放难题:免费Silk v3音频转换全攻略

3分钟搞定微信QQ语音播放难题&#xff1a;免费Silk v3音频转换全攻略 【免费下载链接】silk-v3-decoder [Skype Silk Codec SDK]Decode silk v3 audio files (like wechat amr, aud files, qq slk files) and convert to other format (like mp3). Batch conversion support. …

作者头像 李华
网站建设 2026/5/1 14:23:23

AI工作流配方库:自动化代码审查、测试与迁移的工程实践

1. 项目概述与核心价值如果你是一名开发者&#xff0c;或者你的工作内容里涉及到代码、测试、文档这些技术活儿&#xff0c;那你肯定对“重复劳动”这四个字深恶痛绝。代码审查、写单元测试、版本迁移、安全检查……这些任务既重要又琐碎&#xff0c;消耗着大量的时间和精力。最…

作者头像 李华
网站建设 2026/5/1 14:20:56

python plotly

# 从代码到交互&#xff1a;Python Plotly 的实用解读 1. 它到底是什么 简单说&#xff0c;Plotly 是一个能让静态图表“活起来”的 Python 库。大部分人在做数据可视化时会先想到 Matplotlib&#xff0c;但 Plotly 更像一个“交互式图表工厂”——它生成的图表自带缩放、悬停…

作者头像 李华
网站建设 2026/5/1 14:20:56

中国机器人进军硅谷,每日AI新闻,十大热点

嘿&#xff0c;朋友们&#xff01;今天的AI圈可真是热闹非凡&#xff0c;既有让人热血沸腾的“中国智造”高光时刻&#xff0c;也有让投资者心跳加速的市场震荡。咱们这就把过去24小时最劲爆、最有料的10条科技新闻&#xff0c;用大白话给您盘一盘&#xff01; &#x1f916; …

作者头像 李华