Java编写学生信息管理系统
时间: 2024-01-03 11:04:33 浏览: 71
Java 编写学生信息管理系统
5星 · 资源好评率100%
学生信息管理系统是一种常见的应用,可以使用Java编写。以下是一个简单的Java学生信息管理系统的示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
private ArrayList<Student> students;
public StudentManagementSystem() {
students = new ArrayList<Student>();
}
public void addStudent() {
Scanner input = new Scanner(System.in);
System.out.print("请输入学生姓名:");
String name = input.nextLine();
System.out.print("请输入学生年龄:");
int age = input.nextInt();
System.out.print("请输入学生性别:");
String gender = input.next();
System.out.print("请输入学生学号:");
String id = input.next();
Student student = new Student(name, age, gender, id);
students.add(student);
System.out.println("添加成功!");
}
public void deleteStudent() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要删除的学生学号:");
String id = input.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("没有找到该学生!");
}
public void updateStudent() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要修改的学生学号:");
String id = input.next();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
System.out.print("请输入新的学生姓名:");
String name = input.next();
System.out.print("请输入新的学生年龄:");
int age = input.nextInt();
System.out.print("请输入新的学生性别:");
String gender = input.next();
Student student = new Student(name, age, gender, id);
students.set(i, student);
System.out.println("修改成功!");
return;
}
}
System.out.println("没有找到该学生!");
}
public void queryStudent() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要查询的学生学号:");
String id = input.next();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
System.out.println(students.get(i).toString());
return;
}
}
System.out.println("没有找到该学生!");
}
public void queryAllStudents() {
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i).toString());
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("=====================");
System.out.println("学生信息管理系统");
System.out.println("1. 添加学生");
System.out.println("2. 删除学生");
System.out.println("3. 修改学生");
System.out.println("4. 查询学生");
System.out.println("5. 查询所有学生");
System.out.println("0. 退出系统");
System.out.println("=====================");
System.out.print("请选择操作:");
int choice = input.nextInt();
switch (choice) {
case 1:
system.addStudent();
break;
case 2:
system.deleteStudent();
break;
case 3:
system.updateStudent();
break;
case 4:
system.queryStudent();
break;
case 5:
system.queryAllStudents();
break;
case 0:
System.out.println("感谢使用学生信息管理系统!");
System.exit(0);
break;
default:
System.out.println("无效的选择,请重新选择!");
break;
}
}
}
}
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 void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String toString() {
return "姓名:" + name + ",年龄:" + age + ",性别:" + gender + ",学号:" + id;
}
}
```
此程序提供一些基本操作:添加学生、删除学生、修改学生、查询学生和查询所有学生。您可以自己尝试添加其他功能,如保存和加载数据等。
阅读全文