import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Student {
private int id;
private String name;
private int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
public class StudentManagementSystem {
private static List
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("\n学生信息管理系统");
System.out.println("1. 添加学生");
System.out.println("2. 显示所有学生");
System.out.println("3. 退出");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
addStudent();
break;
case 2:
displayStudents();
break;
case 3:
System.out.println("退出系统。");
return;
default:
System.out.println("无效选择,请重新输入。");
}
}
}
private static void addStudent() {
System.out.println("输入学生 ID:");
int id = scanner.nextInt();
scanner.nextLine();
System.out.println("输入学生姓名:");
String name = scanner.nextLine();
System.out.println("输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine();
students.add(new Student(id, name, age));
System.out.println("学生添加成功!");
}
private static void displayStudents() {
if (students.isEmpty()) {
System.out.println("没有学生信息。");
} else {
for (Student student : students) {
System.out.println(student);
}
}
}
}
标签:java,name,int,程序,System,println,id,out From: https://www.cnblogs.com/xiaoshuanzi/p/18433113