开发一个简单的java系统,该系统可以对学生信息进行增删改查;(用对象数组实现) 要求: (1)录入学生:键盘录入学生信息,使用数组存储学生信息,要求学号不能重复; (2)删除学生:键盘录入要删除的学生学号,将学生从数组中移除; (3)修改学生:键盘录入要修改学生的学号和修改后的信息,将数组中学生的信息修改; (4)遍历学生: 将所有的学生信息输出到控制台; 学生有如下私有字段属性: 整数类型的学号 stuID 字符串类型的姓名 stuName 整数类型的年龄 stuAge 字符串类型的班级名称 stuClass 浮点类型成绩 stuScore;
时间: 2023-06-10 22:08:15 浏览: 80
使用java对学生表进行增删改查
好的,下面是一个简单的实现代码,供参考:
```java
import java.util.Scanner;
public class StudentSystem {
private static final int MAX_STUDENT_NUM = 100; // 最大学生数量
private Student[] students = new Student[MAX_STUDENT_NUM]; // 学生数组
private int studentNum = 0; // 学生数量
public static void main(String[] args) {
StudentSystem system = new StudentSystem();
system.run();
}
// 主程序入口
public void run() {
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 option = scanner.nextInt();
switch (option) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
updateStudent();
break;
case 4:
listStudents();
break;
case 0:
System.out.println("谢谢使用!");
return;
default:
System.out.println("无效操作!");
break;
}
}
}
// 录入学生信息
private void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生信息:");
System.out.print("学号:");
int stuID = scanner.nextInt();
if (findStudentIndex(stuID) != -1) { // 学号重复
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[studentNum++] = new Student(stuID, stuName, stuAge, stuClass, stuScore);
System.out.println("学生信息录入成功!");
}
// 删除学生信息
private void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的学生学号:");
int stuID = scanner.nextInt();
int index = findStudentIndex(stuID);
if (index == -1) { // 学号不存在
System.out.println("学号不存在!");
return;
}
for (int i = index; i < studentNum - 1; i++) {
students[i] = students[i + 1];
}
students[--studentNum] = null;
System.out.println("学生信息删除成功!");
}
// 修改学生信息
private void updateStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要修改的学生学号:");
int stuID = scanner.nextInt();
int index = findStudentIndex(stuID);
if (index == -1) { // 学号不存在
System.out.println("学号不存在!");
return;
}
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[index].setName(stuName);
students[index].setAge(stuAge);
students[index].setClass(stuClass);
students[index].setScore(stuScore);
System.out.println("学生信息修改成功!");
}
// 遍历学生信息
private void listStudents() {
System.out.println("学生信息如下:");
for (int i = 0; i < studentNum; i++) {
System.out.println(students[i]);
}
}
// 查找学生在数组中的索引
private int findStudentIndex(int stuID) {
for (int i = 0; i < studentNum; i++) {
if (students[i].getID() == stuID) {
return i;
}
}
return -1;
}
// 学生类
private static class Student {
private int id;
private String name;
private int age;
private String stuClass;
private float score;
public Student(int id, String name, int age, String stuClass, float score) {
this.id = id;
this.name = name;
this.age = age;
this.stuClass = stuClass;
this.score = score;
}
public int getID() {
return id;
}
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 getClass() {
return stuClass;
}
public void setClass(String stuClass) {
this.stuClass = stuClass;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
@Override
public String toString() {
return "学号:" + id + " 姓名:" + name + " 年龄:" + age + " 班级:" + stuClass + " 成绩:" + score;
}
}
}
```
阅读全文