用java语言设计一个简单的学生信息管理系统,能够实现以下功能:添加学生信息(至少包括姓名、年龄、性别、成绩),删除学生信息(根据姓名,修改学生信息(根据姓名,可修改姓名、年龄、性别、成绩等),查询学生信息(根据姓名,能够输出该学生的所有信息)具有无效输入检测功能并反馈
时间: 2024-01-12 18:05:44 浏览: 59
以下是一个简单的学生信息管理系统的Java代码:
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<Student>();
Scanner scanner = 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 = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
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();
students.add(new Student(name, age, gender, score));
System.out.println("添加成功!");
break;
case 2:
System.out.println("请输入要删除的学生姓名:");
String nameToDelete = scanner.nextLine();
boolean found = false;
for (Student student : students) {
if (student.getName().equals(nameToDelete)) {
students.remove(student);
found = true;
break;
}
}
if (found) {
System.out.println("删除成功!");
} else {
System.out.println("未找到该学生!");
}
break;
case 3:
System.out.println("请输入要修改的学生姓名:");
String nameToModify = scanner.nextLine();
found = false;
for (Student student : students) {
if (student.getName().equals(nameToModify)) {
System.out.println("请输入要修改的信息(姓名、年龄、性别、成绩):");
String infoToModify = scanner.nextLine();
String[] infos = infoToModify.split(" ");
for (String info : infos) {
String[] keyValue = info.split(":");
String key = keyValue[0];
String value = keyValue[1];
switch (key) {
case "姓名":
student.setName(value);
break;
case "年龄":
student.setAge(Integer.parseInt(value));
break;
case "性别":
student.setGender(value);
break;
case "成绩":
student.setScore(Double.parseDouble(value));
break;
default:
System.out.println("无效的键值对:" + info);
break;
}
}
found = true;
break;
}
}
if (found) {
System.out.println("修改成功!");
} else {
System.out.println("未找到该学生!");
}
break;
case 4:
System.out.println("请输入要查询的学生姓名:");
String nameToQuery = scanner.nextLine();
found = false;
for (Student student : students) {
if (student.getName().equals(nameToQuery)) {
System.out.println(student);
found = true;
break;
}
}
if (!found) {
System.out.println("未找到该学生!");
}
break;
case 5:
System.out.println("谢谢使用!");
System.exit(0);
break;
default:
System.out.println("无效的操作!");
break;
}
}
}
}
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;
}
}
此代码中定义了一个Student类,用于存储学生信息,包括姓名、年龄、性别、成绩。主程序中通过一个ArrayList来存储所有学生信息,并提供了添加、删除、修改、查询和退出等操作。其中,修改操作要求输入要修改的信息,格式为“键:值”,例如“姓名:张三 年龄:18 性别:男 成绩:90.0”,可以同时修改多个信息,如果键值对无效,则忽略该键值对。查询操作根据姓名来查询学生信息,输出该学生的所有信息。
阅读全文