写一个java的学生信息管理系统300行代码
时间: 2023-12-18 14:09:38 浏览: 60
好的,以下是一个简单的Java学生信息管理系统,共计约300行代码。
```
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Student> students = new ArrayList<>();
boolean isRunning = true;
while (isRunning) {
System.out.println("请选择操作:");
System.out.println("1.添加学生信息");
System.out.println("2.删除学生信息");
System.out.println("3.查看学生信息");
System.out.println("4.修改学生信息");
System.out.println("5.退出程序");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent(students, scanner);
break;
case 2:
deleteStudent(students, scanner);
break;
case 3:
viewStudent(students);
break;
case 4:
modifyStudent(students, scanner);
break;
case 5:
isRunning = false;
break;
default:
System.out.println("无效的选择,请重新输入。");
break;
}
}
}
private static void addStudent(ArrayList<Student> students, Scanner scanner) {
System.out.println("请输入学生信息:");
System.out.print("姓名:");
String name = scanner.next();
System.out.print("年龄:");
int age = scanner.nextInt();
System.out.print("性别:");
String gender = scanner.next();
System.out.print("学号:");
String id = scanner.next();
students.add(new Student(name, age, gender, id));
System.out.println("添加成功。");
}
private static void deleteStudent(ArrayList<Student> students, Scanner scanner) {
System.out.print("请输入要删除的学生的学号:");
String id = scanner.next();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
students.remove(i);
System.out.println("删除成功。");
return;
}
}
System.out.println("未找到该学生。");
}
private static void viewStudent(ArrayList<Student> students) {
System.out.println("学号\t姓名\t年龄\t性别");
for (Student student : students) {
System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getGender());
}
}
private static void modifyStudent(ArrayList<Student> students, Scanner scanner) {
System.out.print("请输入要修改的学生的学号:");
String id = scanner.next();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
System.out.print("请输入修改后的姓名:");
String name = scanner.next();
System.out.print("请输入修改后的年龄:");
int age = scanner.nextInt();
System.out.print("请输入修改后的性别:");
String gender = scanner.next();
students.set(i, new Student(name, age, gender, id));
System.out.println("修改成功。");
return;
}
}
System.out.println("未找到该学生。");
}
}
class Student {
private String name;
private int age;
private String gender;
private String id;
public Student(String name, int age, String gender, String id) {
this.name = name;
this.age = age;
this.gender = gender;
this.id = id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getId() {
return id;
}
}
```
这个程序实现了基本的学生信息管理功能,包括添加、删除、查看和修改学生信息。其中,学生信息包括姓名、年龄、性别和学号。程序使用了一个ArrayList来存储所有学生的信息,每个学生信息以一个Student对象表示。通过一个无限循环和一个switch语句来实现用户界面和用户操作的处理。
阅读全文