diff --git "a/37 \347\216\213\346\231\264/2022.12.22\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237\345\256\214\345\226\204.md" "b/37 \347\216\213\346\231\264/2022.12.22\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237\345\256\214\345\226\204.md" new file mode 100644 index 0000000000000000000000000000000000000000..63e27a83e1d08019e4166b2d49bc69636f4ee026 --- /dev/null +++ "b/37 \347\216\213\346\231\264/2022.12.22\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237\345\256\214\345\226\204.md" @@ -0,0 +1,167 @@ +## 学生管理系统完善 + +```java +import java.util.Scanner; + +public class StudentSystem { + static Scanner sc = new Scanner(System.in); + static String[] stu = new String[46]; + public static void main(String[] args) { + + + stu[0] = "苏"; + stu[1] = "王"; + stu[2] = "徐"; + stu[3] = "李"; + stu[4] = "刘"; + + aa: while (true){ + Welcome(); + int a = choice(sc.nextInt()); + if (a==1){ + break ;//中止while + } + } + + } + //静态方法只能用static修饰。(静态对象或静态方法) + + //欢迎界面 + public static void Welcome() { + System.out.println( + "\n============================" + + "\n- 欢迎使用3班学生管理系统 --" + + "\n- 1.浏览所有学生信息 -" + + "\n- 2.添加学生信息 -" + + "\n- 3.修改学生信息 -" + + "\n- 4.删除学生信息 -" + + "\n- 5.查询学生信息 -" + + "\n- 6.退出管理系统 -" + + "\n =========================" + + "\n请输入对应的数字选择你需要的功能:"); + } + //菜单选择 + public static int choice(int num){ + int a =0; + switch (num) { + case 1: + //浏览 + Allstudent(); + break; + case 2: + //添加 + addstudent(); + break; + case 3: + //修改 + editStudent(); + break; + case 4: + //删除 + deleteStudent(); + break; + case 5: + //查询 + searchStudent(); + break; + case 6: + //结束 + System.out.println("你选择了退出系统"); + + default: + System.out.println("你输入的选项错误"); + a=1; + } + return a; + } + //删除学生信息 + private static void deleteStudent() { + System.out.println("请问你要删除哪个学生"); + String name= sc.next(); + int index =search(name); + if (index==-1){ + System.out.println("对不起,没有该学生,无法删除"); + }else { + stu[index]=null; + System.out.println("删除成功"); + } + } + //查找学生 + public static void searchStudent(){ + System.out.println("请输入要查找的学生姓名"); + String name =sc.next(); + int index =search(name); + if (index==-1){ + System.out.println("对不起,没有该学生"); + }else { + System.out.println("恭喜,找到了,Ta在第"+(index+1)+"个"); + } + } + //修改 + private static void editStudent(){ + System.out.println("请问你要修改那个学生"); + String name =sc.next(); + int index =search(name); + if (index==-1){ + System.out.println("对不起,没有该学生,无法修改"); + }else { + System.out.println("请问你要把【"+name+"】修改为谁:"); + String newNmae = sc.next(); + stu[index]= newNmae; + System.out.println("修改成功!"); + } + } + //浏览 + public static void Allstudent() { + System.out.println("3班学生如下:"); + int count =0; + for (String name : stu) { + if (name==null){ + count++; + continue; + } + System.out.println(name + "\t"); + } + if (count== stu.length){ + System.out.println("目前还没有学生信息"); + } + } + // 添加 + public static void addstudent() { + System.out.println("请输入你要添加的学生"); + String name = sc.next(); + int index = search( name); + if (index != -1) { + System.out.println("该学生已经在数据库了,请不要重复添加"); + } else { + int nullIndex = search(null); + stu[nullIndex] = name; + System.out.println("添加成功"); + Allstudent(); + + } + } + + public static int search( String str) { + int index = -1; + if (str == null) { + for (int i = 0; i < stu.length; i++) { + if (stu[i]==null){ + index = i; + break; + } + } + }else { + for (int i = 0; i < stu.length; i++) { + if (str.equals(stu[i])) { + index=i; + return index; + } + } + } + return index; + } + +} +``` +