开发一个简单的系统,该系统可以对学生信息进行增删改查;(用对象数组实现) 要求: (1)录入学生:键盘录入学生信息,使用数组存储学生信息,要求学号不能重复; (2)删除学生:键盘录入要删除的学生学号,将学生从数组中移除; (3)修改学生:键盘录入要修改学生的学号和修改后的信息,将数组中学生的信息修改; (4)遍历学生: 将所有的学生信息输出到控制台; 学生有如下私有字段属性: 整数类型的学号 stuID 字符串类型的姓名 stuName 整数类型的年龄 stuAge 字符串类型的班级名称 stuClass 浮点类型成绩 stuScore讲解
时间: 2023-06-11 20:06:47 浏览: 85
简单的学生信息增删改查
好的,这里给出一个简单的示例代码,用对象数组实现学生信息的增删改查:
```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.print("请输入学生学号:");
int id = scanner.nextInt();
// 判断学号是否已存在
for (int i = 0; i < size; i++) {
if (students[i].getStuID() == id) {
System.out.println("学号已存在,请重新输入!");
return;
}
}
System.out.print("请输入学生姓名:");
String name = scanner.next();
System.out.print("请输入学生年龄:");
int age = scanner.nextInt();
System.out.print("请输入学生班级:");
String className = scanner.next();
System.out.print("请输入学生成绩:");
double score = scanner.nextDouble();
students[size++] = new Student(id, name, age, className, score);
System.out.println("学生信息添加成功!");
}
// 删除学生信息
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的学生学号:");
int id = scanner.nextInt();
int index = -1;
for (int i = 0; i < size; i++) {
if (students[i].getStuID() == id) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("学号不存在,请重新输入!");
return;
}
for (int i = index; i < size - 1; i++) {
students[i] = students[i + 1];
}
students[--size] = null;
System.out.println("学生信息删除成功!");
}
// 修改学生信息
public void modifyStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要修改的学生学号:");
int id = scanner.nextInt();
int index = -1;
for (int i = 0; i < size; i++) {
if (students[i].getStuID() == id) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("学号不存在,请重新输入!");
return;
}
System.out.print("请输入学生姓名:");
String name = scanner.next();
System.out.print("请输入学生年龄:");
int age = scanner.nextInt();
System.out.print("请输入学生班级:");
String className = scanner.next();
System.out.print("请输入学生成绩:");
double score = scanner.nextDouble();
students[index].setStuName(name);
students[index].setStuAge(age);
students[index].setStuClass(className);
students[index].setStuScore(score);
System.out.println("学生信息修改成功!");
}
// 遍历学生信息
public void displayStudents() {
System.out.println("学号\t姓名\t年龄\t班级\t成绩");
for (int i = 0; i < size; i++) {
System.out.println(students[i].getStuID() + "\t" + students[i].getStuName() + "\t" +
students[i].getStuAge() + "\t" + students[i].getStuClass() + "\t" + students[i].getStuScore());
}
}
}
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 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;
}
}
```
这里定义了一个 StudentSystem 类来管理学生信息,其中包括了增加、删除、修改和遍历学生信息的方法。另外定义了一个 Student 类来封装学生的属性。在使用对象数组存储学生信息时,需要注意数组大小的设置和已有学生数量的维护。
阅读全文