用java语言设计一个简单的学生信息管理系统,能够实现以下功能:添加学生信息(至少包括姓名、年龄、性别、成绩),删除学生信息(根据姓名,修改学生信息(根据姓名,可修改姓名、年龄、性别、成绩等),查询学生信息(根据姓名,能够输出该学生的所有信息)具有无效输入检测功能并反馈
时间: 2024-01-12 08:05:33 浏览: 14
java课程设计学生信息管理系统.doc
以下是一个简单的学生信息管理系统的Java代码:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
// 存储学生信息的列表
private ArrayList<Student> students;
// 用于从控制台接收输入的Scanner对象
private Scanner scanner;
public StudentManagementSystem() {
students = new ArrayList<>();
scanner = new Scanner(System.in);
}
// 添加学生信息
public void addStudent() {
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入学生性别:");
String gender = scanner.nextLine();
System.out.println("请输入学生成绩:");
double score = scanner.nextDouble();
scanner.nextLine();
Student student = new Student(name, age, gender, score);
students.add(student);
System.out.println("学生信息添加成功!");
}
// 删除学生信息
public void deleteStudent() {
System.out.println("请输入要删除的学生姓名:");
String name = scanner.nextLine();
boolean found = false;
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getName().equals(name)) {
students.remove(i);
found = true;
System.out.println("学生信息删除成功!");
break;
}
}
if (!found) {
System.out.println("没有找到该学生!");
}
}
// 修改学生信息
public void modifyStudent() {
System.out.println("请输入要修改的学生姓名:");
String name = scanner.nextLine();
boolean found = false;
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println("请输入修改后的学生姓名(不修改请输入空格):");
String newName = scanner.nextLine();
if (!newName.equals("")) {
student.setName(newName);
}
System.out.println("请输入修改后的学生年龄(不修改请输入0):");
int newAge = scanner.nextInt();
scanner.nextLine();
if (newAge != 0) {
student.setAge(newAge);
}
System.out.println("请输入修改后的学生性别(不修改请输入空格):");
String newGender = scanner.nextLine();
if (!newGender.equals("")) {
student.setGender(newGender);
}
System.out.println("请输入修改后的学生成绩(不修改请输入0):");
double newScore = scanner.nextDouble();
scanner.nextLine();
if (newScore != 0) {
student.setScore(newScore);
}
found = true;
System.out.println("学生信息修改成功!");
break;
}
}
if (!found) {
System.out.println("没有找到该学生!");
}
}
// 查询学生信息
public void queryStudent() {
System.out.println("请输入要查询的学生姓名:");
String name = scanner.nextLine();
boolean found = false;
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println(student);
found = true;
break;
}
}
if (!found) {
System.out.println("没有找到该学生!");
}
}
// 显示所有学生信息
public void showAllStudents() {
if (students.isEmpty()) {
System.out.println("没有学生信息!");
} else {
for (Student student : students) {
System.out.println(student);
}
}
}
// 启动学生信息管理系统
public void start() {
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. 退出系统");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 0:
System.out.println("谢谢使用学生信息管理系统!");
return;
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
queryStudent();
break;
case 5:
showAllStudents();
break;
default:
System.out.println("无效的选项,请重新输入!");
break;
}
System.out.println();
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
system.start();
}
}
class Student {
private String name;
private int age;
private String gender;
private double score;
public Student(String name, int age, String gender, double score) {
this.name = name;
this.age = age;
this.gender = gender;
this.score = score;
}
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 double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "姓名:" + name + ",年龄:" + age + ",性别:" + gender + ",成绩:" + score;
}
}
```
阅读全文