用java编写程序实现学生信息管理系统
时间: 2023-07-18 20:08:27 浏览: 88
学生信息管理系统+java实现+笔记+源码.zip
5星 · 资源好评率100%
好的,这里提供一个简单的学生信息管理系统的Java代码实现,包括添加学生、修改学生、查询学生和删除学生等功能。
```java
import java.util.Scanner;
import java.util.ArrayList;
public class StudentManagementSystem {
static ArrayList<Student> students = new ArrayList<Student>();
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
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 = input.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
updateStudent();
break;
case 3:
searchStudent();
break;
case 4:
deleteStudent();
break;
case 5:
System.exit(0);
default:
System.out.println("输入错误,请重新输入!");
break;
}
}
}
// 添加学生
public static void addStudent() {
Scanner input = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = input.next();
System.out.println("请输入学生学号:");
String id = input.next();
System.out.println("请输入学生年龄:");
int age = input.nextInt();
System.out.println("请输入学生性别:");
String sex = input.next();
Student student = new Student(name, id, age, sex);
students.add(student);
System.out.println("学生信息添加成功!");
}
// 修改学生
public static void updateStudent() {
Scanner input = new Scanner(System.in);
System.out.println("请输入要修改的学生学号:");
String id = input.next();
Student student = findStudentById(id);
if (student != null) {
System.out.println("请输入学生姓名:");
String name = input.next();
System.out.println("请输入学生年龄:");
int age = input.nextInt();
System.out.println("请输入学生性别:");
String sex = input.next();
student.setName(name);
student.setAge(age);
student.setSex(sex);
System.out.println("学生信息修改成功!");
} else {
System.out.println("未找到该学生信息!");
}
}
// 查询学生
public static void searchStudent() {
Scanner input = new Scanner(System.in);
System.out.println("请输入要查询的学生学号:");
String id = input.next();
Student student = findStudentById(id);
if (student != null) {
System.out.println(student);
} else {
System.out.println("未找到该学生信息!");
}
}
// 删除学生
public static void deleteStudent() {
Scanner input = new Scanner(System.in);
System.out.println("请输入要删除的学生学号:");
String id = input.next();
Student student = findStudentById(id);
if (student != null) {
students.remove(student);
System.out.println("学生信息删除成功!");
} else {
System.out.println("未找到该学生信息!");
}
}
// 根据学号查找学生
public static Student findStudentById(String id) {
for (Student student : students) {
if (student.getId().equals(id)) {
return student;
}
}
return null;
}
}
class Student {
private String name;
private String id;
private int age;
private String sex;
public Student(String name, String id, int age, String sex) {
this.name = name;
this.id = id;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "学生姓名:" + name + ",学号:" + id + ",年龄:" + age + ",性别:" + sex;
}
}
```
可以通过运行该程序,选择不同的操作来添加、修改、查询和删除学生信息。
阅读全文