import java.util.Iterator;
import java.util.Scanner;
import java.util.ArrayList;
class Student{
private int id;
private String name;
private String age;
private String address;
public Student(int id,String name,String age, String address){
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
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 String getAge(){
return age;
}
public void setAge(String age){
this.age = age;
}
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address = address;
}
public String toString(){
return "Student{id = " +getId()+ ", name = " + getName() + ", age = " + getAge() + ", adress = " + getAddress() + "}";
}
}
public class StudentInformation{
public static void main(String[] args){
ArrayList<Student> list = new ArrayList<>();
Scanner sc =new Scanner(System.in);
boolean flag = true;
while(flag){
LoginMenu();
System.out.println("输入你想要的功能");
int input = sc.nextInt();
switch(input){
case 1:
sc.nextLine();
addStudent(list,sc);
break;
case 2:
sc.nextLine();
DeleteStudent( list,sc);
break;
case 3:
sc.nextLine();
ShowStudent(list);
break;
case 4:
sc.nextLine();
CorrectStudent(list,sc);
break;
case 5:
flag = false;
break;
}
}
}
private static void Judge(int id,ArrayList<Student> list){
for (Student student : list) {
if (student.getId() == id) {
System.out.println("重新输入");
return;
}
}
}
private static void LoginMenu(){
System.out.println("1.增加学生");
System.out.println("2.删除学生");
System.out.println("3.显示学生信息");
System.out.println("4.修改学生信息");
System.out.println("5.退出");
}
private static void addStudent(ArrayList<Student> list,Scanner sc) {
System.out.println("输入学生的id");
int id;
while (true) {
if (sc.hasNextInt()) {
id = sc.nextInt();
Judge(id, list);
break;
} else {
System.out.println("请输入一个有效的整数");
sc.next(); // 清空输入缓冲区
}
}// 消耗掉输入缓冲区中的换行符
sc.nextLine();
System.out.println("输入学生的名字");
String name = sc.nextLine();
System.out.println("输入学的年龄:");
String age = sc.nextLine();
System.out.println("输入学生的地址:");
String address = sc.nextLine();
Student student = new Student(id, name, age, address);
list.add(student);
}
private static void DeleteStudent(ArrayList<Student> list,Scanner sc){
/*for(int i = 0;i<list.size();i++){
Student student =list.get(i);
if(student.getId() == id){
list.remove(student);
}
}**/
//当您在循环中使用 list.remove(student); 删除元素时,这会导致 ConcurrentModificationException 异常,因为在迭代过程中不能直接修改列表。为了避免这个问题,可以使用迭代器进行安全的删除操作。
System.out.println("输入想要删除的id");
int id =sc.nextInt();
Iterator<Student> iterator = list.iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
if (student.getId() == id) {
iterator.remove();
System.out.println("学生 " + student.getName() + " 已删除");
}
}
}
private static void CorrectStudent(ArrayList<Student> list,Scanner sc){
//只能整行修改
System.out.println("输入要修改的id");
int id =sc.nextInt();
sc.nextLine();
Student student =list.get(id -1);
if(student.getId() == id){
System.out.println("输入学生的名字");
String name = sc.nextLine();
System.out.println("输入学的年龄:");
String age = sc.nextLine();
System.out.println("输入学生的地址:");
String address =sc.nextLine();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setAddress(address);
}
}
private static void ShowStudent(ArrayList<Student> list){
System.out.println("显示所有信息");
for (Student student : list) {
System.out.println(student.toString());
}
}
}
基于控制台的学生信息管理系统
于 2024-08-04 14:48:44 首次发布