学生管理系统(java数组版)

项目运行结果(具有对学生的 增删该查 功能) 

学生类

//学生类
public class Student {
//    属性private int id;private String name;private int age;//    无参构造public Student() {}
//    满参构造public Student(int id, String name, int age) {this.id = id;this.name = name;this.age = age;}//    getter  setter 方法public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}//    show方法public void show(){System.out.println("学号:" + id + ",姓名:" + name + ",年龄:" + age);}
}

测试类

import java.util.Scanner;public class DemoStudent {public static void main(String[] args) {Scanner sc = new Scanner(System.in);
//        创建数组 储存对象
//        这里是数组方式 完成案例 动态创建数组 创建20个空间 (只能写死空间)Student[] stuArr = new Student[20];while (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("请输入您的选择:(1-5)");int choice = sc.nextInt();
//          判断数组是否为空boolean flag = isEmpty(stuArr);switch (choice){case 1:
//                    System.out.println("添加学生");addStudent(stuArr,sc);System.out.println("输入成功!!");break;case 2:
//                    优化  当数组为空时  不进行删改查操作if (flag){reStudent(sc,stuArr);}else {System.out.println("暂无学生,请先添加");}
//                    System.out.println("删除学生");break;case 3:
//                    System.out.println("修改学生");if (flag){changeStudent(sc,stuArr);}else {System.out.println("暂无学生,请先添加");}break;case 4:
//                    System.out.println("查看学生");if (flag){lookAllStu(stuArr);}else {System.out.println("暂无学生,请先添加");}break;case 5:
//                    System.out.println("退出系统");System.out.println("欢迎您的使用");
//                    退出jvm虚拟机System.exit(0);break;default:System.out.println("输入有误!!请输入(1-5)");break;}}}
//    标记索引位置 返回值public static int isHaveIndex(Student[] stuArr,int newId){
//        遍历数组 进行比较  如果数组中的值不为 null 并且 索引位置的学号 跟 要查询的学号 相同for (int i = 0; i < stuArr.length; i++) {if(stuArr[i] != null && stuArr[i].getId() == newId){
//                放回当前索引位置return i;}}return -1;}
//    添加方法public static void addStudent(Student[] stuArr,Scanner sc){
//        优化 进行判断 是否学号输入重复int id;while (true){System.out.println("请输入学号:");id = sc.nextInt();int index = isHaveIndex(stuArr,id);if(index == -1){
//                不重复break;}else {System.out.println("该学号已经重复!");}}System.out.println("请输入姓名:");String name = sc.next();System.out.println("请输入年龄:");int age = sc.nextInt();
//      满参  封装成一个对象Student stu = new Student(id,name,age);
//        遍历数组 将stu 赋值 给数组中为null的值  赋值后退出循环for (int i = 0; i < stuArr.length; i++) {if(stuArr[i] == null){stuArr[i] = stu;break;}}}//    查看所有学生public static void lookAllStu(Student[] stuArr){
//        遍历数组,将不为null的数组元素 展示为show()for (int i = 0; i < stuArr.length; i++) {if(stuArr[i] != null){stuArr[i].show();}}}
//    删除学生public static void reStudent(Scanner sc,Student[] stuArr){System.out.println("请输入要删除学生的学号");int newId = sc.nextInt();
//        调用方法进行比较  接收放回值int index = isHaveIndex(stuArr,newId);
//        进行判断 如果 index == -1  无数据   否则 将当前 索引 指的数组位置 地址值改为nullif( index == -1){System.out.println("您要删除的学生不存在!!");}else {stuArr[index] = null;System.out.println("删除成功!!");}}//    修改学生public static void changeStudent(Scanner sc,Student[] stuArr){
//        输入要修改的学生学号  遍历数组进行 判断该学生是否存在 如果存在 将将输入的新数据 赋值给当前索引对象System.out.println("请输入要修改的学生学号:");int id = sc.nextInt();//       进行判断int index = isHaveIndex(stuArr,id);if(index == -1){System.out.println("您要修改的学生并没有!!");}else {
//            获取当前索引对象 进行set修改Student stu = stuArr[index];System.out.println("请输入要修改的姓名:");String name = sc.next();System.out.println("请输入要修改的年龄:");int age = sc.nextInt();stu.setName(name);stu.setAge(age);System.out.println("修改成功!!");}}//    判断数组中是否为空  返回值 true falsepublic static boolean isEmpty(Student[] stuArr){
//        遍历 进行比较 如果为空 truefor (int i = 0; i < stuArr.length; i++) {if(stuArr[i] != null){return true;}}return false;}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部