1、Java 的学生信息管理源代码:必须配合学生类才可以运行,学生类在下方,我建议最好还是把学生类分成另一个类来运行比较好!package com.geminno.day12.homework;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOExceptio
2、n;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.ArrayList;import java.util.List;import java.util.Properties;import java.util.Scanner;public class StudentManagement /类private static int ids = 10000;/定义属性private static List datas;/定义集合public static void showStuden
3、ts()/方法for(Student stu : datas)/遍历数组 datasSystem.out.println(stu.getId()+“-“+stu.getName()+“-“+stu.getGender()+“-“+stu.getMajor()+“-“+stu.getAge();/输出结果public static void main(String args) /主方法int input;/定义整型datas = new ArrayList();/实例化对象/初始化数据initData();Scanner s = new Scanner(System.in);/从键盘输入whil
4、e(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(“| 请输入选择(1-6 ): | “);System.out.p
5、rintln(“*“);System.out.print(“请选择您要的操作:“);input = s.nextInt();/从键盘输入switch(input)/switch 语句case 1:/第一种情况System.out.print(“请输入姓名:“);String name = s.next();/输入姓名System.out.print(“请输入性别:“);String gender = s.next();System.out.print(“请输入年龄:“);int age = s.nextInt();System.out.print(“请输入专业:“);String major
6、= s.next();Student stud = new Student(String.valueOf(ids+),name,gender,age,major);/创建一个对象datas.add(stud);System.out.println(“增加成功!“);break;/跳出循环case 2:/情况 2if(datas.size() 0)/如果 datas 长度大于 0showStudents();/则显示学生elseSystem.out.println(“无学生信息!“);/不然就输出break;/跳出循环case 3:/情况 3System.out.print(“请输入您要修改的学
7、生学号:“);String id = s.next();Student stu = findStudentById(id);/修改之前要先查找到该学生boolean flag = false;/定义个标志位if(stu = null)/如果查找的人为空System.out.println(“对不起,没有您要修改的学生 “);/则输出elsewhile (true) /不然进入循环/修改对应的属性System.out.println(“*“);System.out.println(“| 1. 修改姓名 | “);System.out.println(“| 2. 修改年龄 | “);System.
8、out.println(“| 3. 修改性别 | “);System.out.println(“| 4. 修改专业 | “);System.out.println(“| 5. 返回 | “);System.out.println(“| 请输入选择(1-5 ): | “);System.out.println(“*“);System.out.print(“请选择您要的操作:“);int updateInput = s.nextInt();/从键盘输入switch (updateInput) /switch 语句case 1:/修改的情况 1System.out.print(“请输入姓名:“);S
9、tring uname = s.next();stu.setName(uname);/设置名字System.out.println(“修改成功 !“);break;/跳出循环case 2:/修改的情况 2System.out.print(“请输入年龄:“);int uage = s.nextInt();stu.setAge(uage);/设置年龄System.out.println(“修改成功 !“);break;/跳出循环case 3:/修改的情况 3System.out.print(“请输入性别:“);String ugender = s.next();stu.setGender(ugen
10、der);/设置年龄System.out.println(“修改成功 !“);break;/跳出循环case 4:/修改的情况 4System.out.print(“请输入专业:“);String umajor = s.next();stu.setMajor(umajor);/设置专业System.out.println(“修改成功 !“);break;/跳出循环case 5:/修改的情况 5flag = true;/默认标志位为 false 就进入循环,如果为 true,就返回break;/跳出循环default:System.out.println(“您的输入有误 “);/否则就输入有误i
11、f(flag)break;break;case 4:/情况 4System.out.print(“请输入您要删除的学生学号:“);String deleteId = s.next();Student deleteStud = findStudentById(deleteId);/在删除之前要先找到该学生if(deleteStud = null)/如果该学生为空System.out.println(“对不起,没有您要删除的学生 !“);/则输出elsedatas.remove(deleteStud);/否则就删除System.out.println(“删除成功!“);break;/跳出循环cas
12、e 5:/情况 5System.out.print(“请输入您要查找的学生学号:“);String searidId = s.next();Student searchStud = findStudentById(searidId);/要查找的学生,根据 id 进行查找if(searchStud = null)/如果查找的学生没有找到System.out.println(“对不起,没有您要找的学生 !“);/则输出else/否则就显示出信息System.out.println(“您要查找的学生信息如下: “);System.out.println(searchStud.getId()+“-“+
13、searchStud.getName()+“-“+searchStud.getGender()+“-“+searchStud.getMajor()+“-“+searchStud.getAge();break;case 6:/情况 6/保存数据saveDate();/保存System.exit(0);/退出break;default:System.out.println(“您的输入有误“);/否则输入有误private static void initData() /初始化数据try(BufferedReader br = new BufferedReader(new InputStreamRe
14、ader(new FileInputStream(new File(“stud.txt“);/读取)/都是整理输出的结果String str;while(str = br.readLine() != null)/str:Student id=10000, name=jack, gender=男, age=20, major=计算机String studStr = str.substring(str.indexOf(“) + 1, str.lastIndexOf(“);/studStr:id=10000, name=jack, gender=男, age=20, major=计算机String
15、studArray = studStr.split(“,“);/id=10000 name=jack gender=男 age=20 major=计算机Student obj = new Student();for(int i = 0;i studArray.length;i+)if(i = 0)String id = studArrayi.split(“=“);/id 10000obj.setId(id1);else if(i = 1)String name = studArrayi.split(“=“);/name jackobj.setName(name1);else if(i = 2)
16、String gender = studArrayi.split(“=“);/ gender 男obj.setGender(gender1);else if(i = 3)String age = studArrayi.split(“=“);/age 20obj.setAge(Integer.parseInt(age1);else if(i = 4)String major = studArrayi.split(“=“);/major 计算机obj.setMajor(major1);datas.add(obj);/解决 id 重复的问题Student lastStud = datas.get(d
17、atas.size() - 1);int lastId = Integer.parseInt(lastStud.getId();ids = +lastId; catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();private static void saveDate() try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new Fil
18、e(“stud.txt“);/写出)/datasfor(Student stud : datas)/遍历 datasbw.write(stud.toString();/写出到 stud.txt 文件中bw.newLine();/换行catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e1) e1.printStackTrace();private static Student findStudentById(String id) for(Student stud : datas)/遍历 datasif(
19、stud.getId().equals(id)/如果找的 id 和本身的 id 相等则返回 studreturn stud;return null;这是学生类:public class Student private String id;private String name;private String gender;private int age;private String major;Overridepublic String toString() return “Student id=“ + id + “, name=“ + name + “, gender=“ + gender+
20、“, age=“ + age + “, major=“ + major + “;Overridepublic int hashCode() final int prime = 31;int result = 1;result = prime * result + (id = null) ? 0 : id.hashCode();return result;Overridepublic boolean equals(Object obj) if (this = obj)return true;if (obj = null)return false;if (getClass() != obj.get
21、Class()return false;Student other = (Student) obj;if (id = null) if (other.id != null)return false; else if (!id.equals(other.id)return false;return true;public String getId() return id;public void setId(String id) this.id = id;public String getName() return name;public void setName(String name) thi
22、s.name = name;public String getGender() return gender;public void setGender(String gender) this.gender = gender;public int getAge() return age;public void setAge(int age) this.age = age;public String getMajor() return major;public void setMajor(String major) this.major = major;public Student(String id, String name, String gender, int age, String major) super();this.id = id;this.name = name;this.gender = gender;this.age = age;this.major = major;public Student()