ArrayList<Student> array = new ArrayList<Student>();
while (true){ System.out.println("0");System.out.println("1");
System.out.println("3");System.out.println("2");System.out.println("4")
System.out.println("5退出");Scanner sc = new Scanner(System.in);
String aString = sc.nextLine();switch (aString) {
case "1":findAllStudent(array); break;case "2":addStudent(array);
break;case "3":deleteStudent(array);break;case "4":
case "5":break;default:System.out.println("");
System.exit(0); break;}}}public static void updateStudent(ArrayList<Student>array){Scanner sc=new Scanner(System.in);
System.out.println("需要修改的学号");String id =sc.nextLine();int index=-1;
for (int i = 0; i < array.size(); i++) {Student s =array.get(i);if(s.getId().equals(id)){
index=i;break;}} if(index==-1){System.out.println("");
}else{System.out.println(":");String name =sc.nextLine();
System.out.println("String age =sc.nextLine();System.out.println("
String address =sc.nextLine();Student s =new Student();
array.set(index, s);System.out.println(""); }}
public static void deleteStudent(ArrayList<Student>array){
Scanner sc=new Scanner(System.in);System.out.println(":");
String id =sc.nextLine(); int index=-1;
for (int i = 0; i < array.size(); i++) {Student s =array.get(i);
if(s.getId().equals(id)){ index=i; break;} }if(index==-1){
System.out.println("");}else{
array.remove(index);
System.out.println(""); }}
public static void addStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
String id;while (true) {System.out.println(":"); id = sc.nextLine();
boolean flag = falsefor (int x = 0; x < array.size(); x++) {Student s = array.get(x);
if (s.getId().equals(id)) { flag = true;break; } }
if (flag) {
System.out.println("");
} else {
break
}
}
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄");
String age = sc.nextLine();
System.out.println("请输入学生居住地");
String address = sc.nextLine();
// 创建学生对象
Student s = new Student();
s.setId(id);
s.setName(name);
s.setAge(age);
s.setAddress(address);
array.add(s);
System.out.println("添加学生成功");
}
public static void findAllStudent(ArrayList<Student> array) {
if (array.size() == 0) {
System.out.println("还没录入信息");
return;
}
System.out.println("学号\t姓名\t年龄\t居住地");
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
}
}
}
|
|