开发一个简单的java系统,该系统可以对学生信息进行增删改查;(用对象数组实现) 要求: (1)录入学生:键盘录入学生信息,使用数组存储学生信息,要求学号不能重复; (2)删除学生:键盘录入要删除的学生学号,将学生从数组中移除; (3)修改学生:键盘录入要修改学生的学号和修改后的信息,将数组中学生的信息修改; (4)遍历学生: 将所有的学生信息输出到控制台; 学生有如下私有字段属性: 整数类型的学号 stuID 字符串类型的姓名 stuName 整数类型的年龄 stuAge 字符串类型的班级名称 stuClass 浮点类型成绩 stuScore;
时间: 2023-06-10 14:08:19 浏览: 589
以下是一个简单的Java系统,使用对象数组实现对学生信息的增删改查:
```java
import java.util.Scanner;
public class StudentSystem {
private Student[] students; // 学生数组
private int size; // 学生数量
// 构造函数,初始化学生数组和数量
public StudentSystem(int capacity) {
students = new Student[capacity];
size = 0;
}
// 录入学生信息
public void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号:");
int stuID = scanner.nextInt();
scanner.nextLine(); // 将换行符读取掉
System.out.println("请输入姓名:");
String stuName = scanner.nextLine();
System.out.println("请输入年龄:");
int stuAge = scanner.nextInt();
scanner.nextLine(); // 将换行符读取掉
System.out.println("请输入班级:");
String stuClass = scanner.nextLine();
System.out.println("请输入成绩:");
double stuScore = scanner.nextDouble();
// 判断学号是否重复
for (int i = 0; i < size; i++) {
if (students[i].getStuID() == stuID) {
System.out.println("学号重复,请重新输入!");
return;
}
}
// 创建学生对象并添加到数组中
Student student = new Student(stuID, stuName, stuAge, stuClass, stuScore);
students[size++] = student;
System.out.println("添加成功!");
}
// 删除学生信息
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生学号:");
int stuID = scanner.nextInt();
// 查找学生在数组中的位置
int index = -1;
for (int i = 0; i < size; i++) {
if (students[i].getStuID() == stuID) {
index = i;
break;
}
}
// 如果找到学生,则将其从数组中移除
if (index != -1) {
for (int i = index; i < size - 1; i++) {
students[i] = students[i + 1];
}
students[--size] = null;
System.out.println("删除成功!");
} else {
System.out.println("未找到该学生!");
}
}
// 修改学生信息
public void updateStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的学生学号:");
int stuID = scanner.nextInt();
// 查找学生在数组中的位置
int index = -1;
for (int i = 0; i < size; i++) {
if (students[i].getStuID() == stuID) {
index = i;
break;
}
}
// 如果找到学生,则修改其信息
if (index != -1) {
System.out.println("请输入修改后的姓名:");
String stuName = scanner.nextLine();
System.out.println("请输入修改后的年龄:");
int stuAge = scanner.nextInt();
scanner.nextLine(); // 将换行符读取掉
System.out.println("请输入修改后的班级:");
String stuClass = scanner.nextLine();
System.out.println("请输入修改后的成绩:");
double stuScore = scanner.nextDouble();
students[index].setStuName(stuName);
students[index].setStuAge(stuAge);
students[index].setStuClass(stuClass);
students[index].setStuScore(stuScore);
System.out.println("修改成功!");
} else {
System.out.println("未找到该学生!");
}
}
// 遍历学生信息
public void listStudents() {
if (size == 0) {
System.out.println("没有学生信息!");
} else {
System.out.println("学号\t姓名\t年龄\t班级\t成绩");
for (int i = 0; i < size; i++) {
System.out.println(students[i]);
}
}
}
public static void main(String[] args) {
StudentSystem studentSystem = new StudentSystem(100);
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("0. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
studentSystem.addStudent();
break;
case 2:
studentSystem.deleteStudent();
break;
case 3:
studentSystem.updateStudent();
break;
case 4:
studentSystem.listStudents();
break;
case 0:
System.exit(0);
break;
default:
System.out.println("无效操作!");
break;
}
}
}
}
class Student {
private int stuID;
private String stuName;
private int stuAge;
private String stuClass;
private double stuScore;
public Student(int stuID, String stuName, int stuAge, String stuClass, double 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 double getStuScore() {
return stuScore;
}
public void setStuScore(double stuScore) {
this.stuScore = stuScore;
}
@Override
public String toString() {
return stuID + "\t" + stuName + "\t" + stuAge + "\t" + stuClass + "\t" + stuScore;
}
}
```
阅读全文