开发一个简单的系统,该系统可以对学生信息进行增删改查;(用对象数组实现) 要求: (1)录入学生:键盘录入学生信息,使用数组存储学生信息,要求学号不能重复; (2)删除学生:键盘录入要删除的学生学号,将学生从数组中移除; (3)修改学生:键盘录入要修改学生的学号和修改后的信息,将数组中学生的信息修改; (4)遍历学生: 将所有的学生信息输出到控制台; 学生有如下私有字段属性: 整数类型的学号 stuID 字符串类型的姓名 stuName 整数类型的年龄 stuAge 字符串类型的班级名称 stuClass 浮点类型成绩 stuScore;
时间: 2023-06-11 15:06:56 浏览: 78
以下是一个简单的实现代码,使用了对象数组来存储学生信息:
```java
import java.util.Scanner;
public class StudentManagementSystem {
private static final int MAX_STUDENTS = 100; // 最大学生数量
private Student[] students; // 存储学生信息的数组
private int numStudents; // 当前学生数量
public StudentManagementSystem() {
students = new Student[MAX_STUDENTS];
numStudents = 0;
}
// 添加学生
public void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生信息:");
System.out.print("学号:");
int stuID = scanner.nextInt();
// 检查学号是否已经存在
for (int i = 0; i < numStudents; i++) {
if (students[i].getStuID() == stuID) {
System.out.println("学号已经存在,请重新输入!");
return;
}
}
System.out.print("姓名:");
String stuName = scanner.next();
System.out.print("年龄:");
int stuAge = scanner.nextInt();
System.out.print("班级:");
String stuClass = scanner.next();
System.out.print("成绩:");
float stuScore = scanner.nextFloat();
students[numStudents++] = new Student(stuID, stuName, stuAge, stuClass, stuScore);
System.out.println("添加成功!");
}
// 删除学生
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的学生学号:");
int stuID = scanner.nextInt();
for (int i = 0; i < numStudents; i++) {
if (students[i].getStuID() == stuID) {
// 将要删除的学生从数组中移除
for (int j = i; j < numStudents - 1; j++) {
students[j] = students[j + 1];
}
students[--numStudents] = null;
System.out.println("删除成功!");
return;
}
}
System.out.println("学号不存在!");
}
// 修改学生信息
public void updateStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要修改的学生学号:");
int stuID = scanner.nextInt();
for (int i = 0; i < numStudents; i++) {
if (students[i].getStuID() == stuID) {
System.out.println("请输入修改后的学生信息:");
System.out.print("姓名:");
String stuName = scanner.next();
System.out.print("年龄:");
int stuAge = scanner.nextInt();
System.out.print("班级:");
String stuClass = scanner.next();
System.out.print("成绩:");
float stuScore = scanner.nextFloat();
students[i].setStuName(stuName);
students[i].setStuAge(stuAge);
students[i].setStuClass(stuClass);
students[i].setStuScore(stuScore);
System.out.println("修改成功!");
return;
}
}
System.out.println("学号不存在!");
}
// 遍历学生信息
public void displayStudents() {
System.out.println("学生信息如下:");
System.out.println("学号\t姓名\t年龄\t班级\t成绩");
for (int i = 0; i < numStudents; i++) {
System.out.println(students[i].getStuID() + "\t" +
students[i].getStuName() + "\t" +
students[i].getStuAge() + "\t" +
students[i].getStuClass() + "\t" +
students[i].getStuScore());
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
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();
switch (choice) {
case 1:
system.addStudent();
break;
case 2:
system.deleteStudent();
break;
case 3:
system.updateStudent();
break;
case 4:
system.displayStudents();
break;
case 5:
System.exit(0);
break;
default:
System.out.println("输入有误,请重新选择!");
break;
}
}
}
}
class Student {
private int stuID; // 学号
private String stuName; // 姓名
private int stuAge; // 年龄
private String stuClass; // 班级
private float stuScore; // 成绩
public Student(int stuID, String stuName, int stuAge, String stuClass, float stuScore) {
this.stuID = stuID;
this.stuName = stuName;
this.stuAge = stuAge;
this.stuClass = stuClass;
this.stuScore = stuScore;
}
public int getStuID() {
return stuID;
}
public void setStuID(int stuID) {
this.stuID = stuID;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getStuClass() {
return stuClass;
}
public void setStuClass(String stuClass) {
this.stuClass = stuClass;
}
public float getStuScore() {
return stuScore;
}
public void setStuScore(float stuScore) {
this.stuScore = stuScore;
}
}
```
这个程序运行后,可以让用户选择添加、删除、修改或遍历学生信息,根据用户的选择进行相应的操作。
阅读全文