学生信息管理:包括添加、修改、删除学生信息、查询学生信息等功能,实现学生信息的录入和管理。编译成Java代码
时间: 2023-12-14 20:05:11 浏览: 111
学生信息管理(学生信息的录入、修改、查询、删除)
以下是一个简单的 Java 代码示例,实现学生信息的录入和管理,包括添加、修改、删除学生信息、查询学生信息等功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagement {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Student> students = new ArrayList<Student>();
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(students, input);
break;
case 2:
updateStudent(students, input);
break;
case 3:
deleteStudent(students, input);
break;
case 4:
queryStudent(students, input);
break;
case 5:
System.out.println("程序已退出。");
System.exit(0);
break;
default:
System.out.println("无效的选择,请重新输入。");
}
}
}
public static void addStudent(ArrayList<Student> students, Scanner input) {
System.out.println("请输入学生的姓名:");
String name = input.next();
System.out.println("请输入学生的年龄:");
int age = input.nextInt();
System.out.println("请输入学生的性别(男/女):");
String gender = input.next();
System.out.println("请输入学生的班级:");
String className = input.next();
Student newStudent = new Student(name, age, gender, className);
students.add(newStudent);
System.out.println("添加学生信息成功。");
}
public static void updateStudent(ArrayList<Student> students, Scanner input) {
System.out.println("请输入要修改学生的姓名:");
String name = input.next();
boolean isFound = false;
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println("请输入学生的年龄:");
int age = input.nextInt();
student.setAge(age);
System.out.println("请输入学生的性别(男/女):");
String gender = input.next();
student.setGender(gender);
System.out.println("请输入学生的班级:");
String className = input.next();
student.setClassName(className);
System.out.println("修改学生信息成功。");
isFound = true;
break;
}
}
if (!isFound) {
System.out.println("未找到该学生的信息。");
}
}
public static void deleteStudent(ArrayList<Student> students, Scanner input) {
System.out.println("请输入要删除学生的姓名:");
String name = input.next();
boolean isFound = false;
for (Student student : students) {
if (student.getName().equals(name)) {
students.remove(student);
System.out.println("删除学生信息成功。");
isFound = true;
break;
}
}
if (!isFound) {
System.out.println("未找到该学生的信息。");
}
}
public static void queryStudent(ArrayList<Student> students, Scanner input) {
System.out.println("请输入要查询学生的姓名:");
String name = input.next();
boolean isFound = false;
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println("学生姓名:" + student.getName());
System.out.println("学生年龄:" + student.getAge());
System.out.println("学生性别:" + student.getGender());
System.out.println("学生班级:" + student.getClassName());
isFound = true;
break;
}
}
if (!isFound) {
System.out.println("未找到该学生的信息。");
}
}
}
class Student {
private String name;
private int age;
private String gender;
private String className;
public Student(String name, int age, String gender, String className) {
this.name = name;
this.age = age;
this.gender = gender;
this.className = className;
}
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 getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
```
这个程序使用了一个 `Student` 类来表示学生信息,使用 `ArrayList` 来存储多个学生的信息。主程序通过不断循环实现菜单选择,根据用户的选择来执行不同的操作。具体实现了添加学生、修改学生、删除学生和查询学生信息四个功能。
阅读全文