java学生信息管理系统
时间: 2023-10-14 11:07:25 浏览: 68
javaWeb,基于SSM+mysql的经典学生管理系统项目源码添加学生信息、学生信息列表、登录、中注册、更新学生信息
一个简单的Java学生信息管理系统可以包含以下功能:
1. 添加学生信息:包括学生姓名,学号,性别,年龄,联系方式等;
2. 查询学生信息:可以按照学号或姓名进行查询,并显示查询结果;
3. 修改学生信息:可以修改学生的个人信息,如联系方式、年龄等;
4. 删除学生信息:可以删除指定学生的信息;
5. 显示所有学生信息:以列表形式展示所有学生的信息。
以下是一个简单的Java代码实现:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
private ArrayList<Student> students = new ArrayList<Student>();
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
system.start();
}
public void start() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入要执行的操作:");
System.out.println("1.添加学生信息 2.查询学生信息 3.修改学生信息 4.删除学生信息 5.显示所有学生信息 6.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
searchStudent();
break;
case 3:
modifyStudent();
break;
case 4:
deleteStudent();
break;
case 5:
displayAllStudents();
break;
case 6:
System.exit(0);
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
// 添加学生信息
public void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生学号:");
String id = scanner.nextLine();
System.out.println("请输入学生性别:");
String gender = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入学生联系方式:");
String phone = scanner.nextLine();
students.add(new Student(name, id, gender, age, phone));
System.out.println("添加成功!");
}
// 查询学生信息
public void searchStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的学生姓名或学号:");
String keyword = scanner.nextLine();
ArrayList<Student> results = new ArrayList<Student>();
for (Student student : students) {
if (student.getName().equals(keyword) || student.getId().equals(keyword)) {
results.add(student);
}
}
if (results.size() == 0) {
System.out.println("未找到符合条件的学生!");
} else {
System.out.println("查询结果如下:");
for (Student student : results) {
System.out.println(student.toString());
}
}
}
// 修改学生信息
public void modifyStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的学生学号:");
String id = scanner.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println("请输入修改后的学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入修改后的学生性别:");
String gender = scanner.nextLine();
System.out.println("请输入修改后的学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入修改后的学生联系方式:");
String phone = scanner.nextLine();
student.setName(name);
student.setGender(gender);
student.setAge(age);
student.setPhone(phone);
System.out.println("修改成功!");
return;
}
}
System.out.println("未找到指定的学生!");
}
// 删除学生信息
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生学号:");
String id = scanner.nextLine();
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 displayAllStudents() {
if (students.size() == 0) {
System.out.println("暂无学生信息!");
} else {
System.out.println("所有学生信息如下:");
for (Student student : students) {
System.out.println(student.toString());
}
}
}
}
class Student {
private String name;
private String id;
private String gender;
private int age;
private String phone;
public Student(String name, String id, String gender, int age, String phone) {
this.name = name;
this.id = id;
this.gender = gender;
this.age = age;
this.phone = phone;
}
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String toString() {
return "姓名:" + name + " 学号:" + id + " 性别:" + gender + " 年龄:" + age + " 联系方式:" + phone;
}
}
```
这是一个基础版本的学生信息管理系统,可以根据实际需求进行扩展。
阅读全文