把方法放在一个class类里面,稍后修改;
import java.util.Scanner;
public class studentmanger {
public static void main(String[] args) {
//定义学生数据的存储容器
Student[] stus = new Student[100];
int size = 0;
//2、显示功能菜单
System.out.println("欢迎使用学生管理系统");
System.out.println("1、添加学生");
System.out.println("2、删除学生");
System.out.println("3、修改学生");
System.out.println("4、查询学生");
System.out.println("5、退出系统");
//3、接收用户输入的选择
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
//4、根据用户的选择,调用不同的功能
switch (choice) {
case 1:
System.out.println("添加学生");
//从键盘录入一个字符串,包含学号、姓名、年龄,用逗号隔开,将学生信息拆分,封装到一个student对象中
//将student对象存储到数组中
size = addStudent(stus, size);
break;
case 2:
System.out.println("删除学生");
//输入一个学生的学号,将数组中的学生信息删除
break;
case 3:
System.out.println("修改学生");
//输入一个学生的学号,将数组中的学生信息修改
break;
case 4:
System.out.println("查询学生");
//输入一个学生的学号,将数组中的学生信息查询出来
break;
case 5:
System.out.println("退出系统");
break;
default:
System.out.println("输入有误");
break;
}
}
public static int addStudent(Student[] stus, int size) {
//从键盘录入一个字符串,包含学号、姓名、年龄,用逗号隔开,将学生信息拆分,封装到一个student对象中
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] arr = str.split(",");
Student student = new Student(arr[0], arr[1], Integer.parseInt(arr[2]));
//将student对象存储到数组中
stus[size] = student;
size++;
return size;
}
public static void deleteStudent(Student[] stus) {
//输入一个学生的学号,将数组中的学生信息删除
Scanner sc = new Scanner(System.in);
String sno = sc.nextLine();
for (int i = 0; i < stus.length; i++) {
if (stus[i] != null && stus[i].sno.equals(sno)) {
stus[i] = null;
break;
}
}
}
public static Student queryStudent(Student[] stus) {
//输入一个学生的学号,将数组中的学生信息查询出来
Scanner sc = new Scanner(System.in);
String sno = sc.nextLine();
for (int i = 0; i < stus.length; i++) {
if (stus[i] != null && stus[i].sno.equals(sno)) {
return stus[i];
}
}
return null;
}
}