请根据school类中这段代码:public void removeStudent(Student student) { studentList.remove(student); } public void modigyStudentInfo(Student student, String newStudentID, String newName) { student.setStudentID(newStudentID); student.setName(newName); }修改主类中下面这段代码:case 5: System.out.println("请输入学号:"); studentID=scanner.next(); Student student1=new Student(); school.removeStudent(student1); break; case 6: System.out.println("请输入学号:"); studentID=scanner.next(); System.out.println("请输入新学号:"); String newStudentID=scanner.next(); System.out.println("请输入新姓名:"); String newName=scanner.next(); Student student2=new Student(); school.modigyStudentInfo(student2, newStudentID, newName); break;
时间: 2024-03-28 18:40:31 浏览: 58
您需要对主类中的代码进行修改,使其可以正确地调用school类中的removeStudent和modigyStudentInfo方法。具体修改如下:
case 5:
System.out.println("请输入学号:");
studentID = scanner.next();
Student student1 = null;
for (Student s : school.getStudentList()) {
if (s.getStudentID().equals(studentID)) {
student1 = s;
break;
}
}
if (student1 == null) {
System.out.println("没有找到该学生!");
} else {
school.removeStudent(student1);
System.out.println("删除成功!");
}
break;
case 6:
System.out.println("请输入学号:");
studentID = scanner.next();
System.out.println("请输入新学号:");
String newStudentID = scanner.next();
System.out.println("请输入新姓名:");
String newName = scanner.next();
Student student2 = null;
for (Student s : school.getStudentList()) {
if (s.getStudentID().equals(studentID)) {
student2 = s;
break;
}
}
if (student2 == null) {
System.out.println("没有找到该学生!");
} else {
school.modigyStudentInfo(student2, newStudentID, newName);
System.out.println("修改成功!");
}
break;
在case 5中,我们首先通过循环找到需要删除的学生对象,然后再调用removeStudent方法进行删除。在case 6中,同样是通过循环找到需要修改的学生对象,然后再调用modigyStudentInfo方法进行修改。同时,我们还加入了一些错误处理的代码,以防止用户输入错误的学号。
阅读全文