今天,进行了新的测试。
题目为简单的学生管理系统,包含增删改查。
点击查看代码
//张一衡 20234023
import java.util.ArrayList;
import java.util.Scanner;
public class Moontest {
public static void main(String[] args) {
StudentManager studentManager = new StudentManager();
studentManager.runMenu();
}
}
class Student {
private String stunumber;
private String name;
private int age;
private boolean sex;
private double score;
public Student() {}
public Student(String stunumber, String name, int age, boolean sex, double score) {
this.stunumber=stunumber;
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
}
// Get and Set
public String getStunumber() {
return stunumber;
}
public void setStunumber(String stunumber) {
this.stunumber = stunumber;
}
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 boolean getSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
class StudentManager {
private ArrayList<Student> students = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);
void showStudent() {
if(students.isEmpty()) {
System.out.println("学生信息为空!");
}else{
for(Student student:students) {
System.out.println("*************************************");
System.out.println("学号为: " + student.getStunumber());
System.out.println("姓名为: " + student.getName());
System.out.println("年龄为: " + student.getAge());
System.out.println("性别为: " + (student.getSex() ? "男" : "女"));
System.out.println("分数为: " + student.getScore());
System.out.println("*************************************");
}
}
}
void addStudent(String stunumber, String name, int age, boolean sex, double score) {
for (Student student : students) {
if (student.getStunumber().equals(stunumber)) {
System.out.println("学号重复!");
return;
}
}
students.add(new Student(stunumber, name, age, sex, score));
}
void deleteStudent(String stunumber) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getStunumber().equals(stunumber)) {
students.remove(i);
System.out.println("学生信息已删除。");
return;
}
}
System.out.println("未找到该学号的学生。");
}
void updateStudent(String stunumber) {
for (Student student : students) {
if (student.getStunumber().equals(stunumber)) {
System.out.println("请输入你要修改的学生信息" +"(学号,姓名,年龄,性别(1 = 男 / 0 = 女),成绩):");
String newStunumber = scanner.nextLine();
String changename = scanner.nextLine();
int changeage = scanner.nextInt();
boolean changesex = scanner.nextInt() == 1;
double changescore = scanner.nextDouble();
student.setStunumber(newStunumber);
student.setName(changename);
student.setAge(changeage);
student.setSex(changesex);
student.setScore(changescore);
System.out.println("学生信息已更新。");
return;
}else{
System.out.println("未找到该学号的学生。");
}
}
}
void selectStudent(String stunumber) {
for (Student student : students) {
if (student.getStunumber().equals(stunumber)) {
System.out.println("*************************************");
System.out.println("学号为: " + student.getStunumber());
System.out.println("姓名为: " + student.getName());
System.out.println("年龄为: " + student.getAge());
System.out.println("性别为: " + (student.getSex() ? "男" : "女"));
System.out.println("成绩为: " + student.getScore());
System.out.println("*************************************");
return;
}
}
System.out.println("未找到该学号的学生。");
}
void showMenu() {
System.out.println("***************************************************************");
System.out.println(" 石家庄铁道大学软件工程系学生信息管理系统");
System.out.println("***************************************************************");
System.out.println(" 1、遍历输出学生信息:");
System.out.println(" 2、新学生信息录入:");
System.out.println(" 3、删除学生信息:");
System.out.println(" 4、修改学生信息:");
System.out.println(" 5、查询学生信息:");
System.out.println("***************************************************************");
}
void runMenu() {
do {
showMenu();
System.out.print("请输入你的选择: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
showStudent();
break;
case 2:
System.out.println("请输入你要添加的学生信息(学号,姓名,年龄,性别(1 = 男 / 0 = 女),成绩):");
String addnumber = scanner.nextLine();
String addname = scanner.nextLine();
int addage = scanner.nextInt();
boolean addsex = scanner.nextInt() == 1;
double addscore = scanner.nextDouble();
addStudent(addnumber, addname, addage, addsex, addscore);
break;
case 3:
System.out.print("请输入你要删除的学生学号:");
String dnumber = scanner.nextLine();
deleteStudent(dnumber);
break;
case 4:
System.out.println("请输入你要修改学生信息的学号:");
String changestunumber=scanner.nextLine();
updateStudent(changestunumber);
break;
case 5:
System.out.print("请输入你要查询的学生学号:");
String selnumber = scanner.nextLine();
selectStudent(selnumber);
break;
default:
System.out.println("该选项不存在,请你重新输入!");
}
} while (true);
}
}
在代码中我运用了ArrayList来进行空间部署以处理定量空间问题,当然数组也是可以处理的。
以下是用数组的处理方式,大同小异,只是用Count计数来处理有效问题。
点击查看代码
```import java.util.Scanner;
public class Moontest {
public static void main(String[] args) {
StudentManager studentManager = new StudentManager();
studentManager.runMenu();
}
}
class Student {
private String stunumber;
private String name;
private int age;
private boolean sex;
private double score;
public Student() {}
public Student(String stunumber, String name, int age, boolean sex, double score) {
this.stunumber = stunumber;
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
}
// Getters and Setters
public String getStunumber() {
return stunumber;
}
public void setStunumber(String stunumber) {
this.stunumber = stunumber;
}
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 boolean getSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
class StudentManager {
private Student[] students = new Student[5];
private int studentCount = 0; // Keep track of the number of students
private Scanner scanner = new Scanner(System.in);
void showStudent() {
if (studentCount == 0) {
System.out.println("学生信息为空!");
return;
}
for (int i = 0; i < studentCount; i++) {
Student student = students[i];
System.out.println("*************************************");
System.out.println("学号为: " + student.getStunumber());
System.out.println("姓名为: " + student.getName());
System.out.println("年龄为: " + student.getAge());
System.out.println("性别为: " + (student.getSex() ? "男" : "女"));
System.out.println("分数为 + student.getScore());
System.out.println("*************************************");
}
}
void addStudent(String stunumber, String name, int age, boolean sex, double score) {
if (studentCount >= students.length) {
System.out.println("学生人数已满,无法添加更多学生!");
return;
}
for (int i = 0; i < studentCount; i++) {
if (students[i].getStunumber().equals(stunumber)) {
System.out.println("学号重复!");
return;
}
}
students[studentCount] = new Student(stunumber, name, age, sex, score);
studentCount++;
System.out.println("学生信息添加成功!");
}
void deleteStudent(String stunumber) {
for (int i = 0; i < studentCount; i++) {
if (students[i].getStunumber().equals(stunumber)) {
// Shift remaining elements to the left
for (int j = i; j < studentCount - 1; j++) {
students[j] = students[j + 1];
}
students[studentCount - 1] = null; // Clear the last element
studentCount--;
System.out.println("学生信息已删除。");
return;
}
}
System.out.println("未找到该学号的学生。");
}
void updateStudent(String stunumber, String name, int age, boolean sex, double score) {
for (int i = 0; i < studentCount; i++) {
if (students[i].getStunumber().equals(stunumber)) {
students[i].setName(name);
students[i].setAge(age);
students[i].setSex(sex);
students[i].setScore(score);
System.out.println("学生信息已更新。");
return;
}
}
System.out.println("未找到该学号的学生。");
}
void selectStudent(String stunumber) {
Count; i++) {
if (students[i].getStunumber().equals(stunumber)) {
Student student = students[i];
System.out.println("");
System.out.println("学号为: " + student.getStunumber());
System.out.println("姓名为: " + student.getName());
System.out.println("年龄为: " + student.getAge());
System.out.println("性别为: " + (student.getSex() ? "男" : "女"));
System.out.println("成绩为: " + student.getScore());
System.out.println("");
return;
}
}
System.out.println("未找到该学号的学生。");
}
void showMenu() {
System.out.println("***************************************************************");
System.out.println(" 石家庄铁道大学软件工程系学生信息管理系统");
System.out.println("***************************************************************");
System.out.println(" 1、遍历输出学生信息:");
System.out.println(" 2、新学生信息录入:");
System.out.println(" 3、删除学生信息:");
System.out.println(" 4、修改学生信息:");
System.out.println(" 5、查询学生信息:");
System.out.println("***************************************************************");
}
void runMenu() {
do {
showMenu();
System.out.print("请输入你的选择: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
showStudent();
break;
case 2:
System.out.println("请输入你要添加的学生信息(学号,姓名,年龄,性别(1 = 男 / 0 = 女),成绩):");
String addnumber = scanner.nextLine();
String addname = scanner.nextLine();
int addage = scanner.nextInt();
boolean addsex = scanner.nextInt() == 1;
double addscore = scanner.nextDouble();
addStudent(addnumber, addname, addage, addsex, addscore);
break;
case 3:
System.out.print("请输入你要删除的学生学号:");
String dnumber = scanner.nextLine();
deleteStudent(dnumber);
break;
case 4:
System.out.println("请输入你要修改的学生信息(学号,姓名,年龄,性别(1 = 男 / 0 = 女),成绩):");
String changestunumber = scanner.nextLine();
String changename = scanner.nextLine();
int changeage = scanner.nextInt();
boolean changesex = scanner.nextInt() == 1;
double changescore = scanner.nextDouble();
updateStudent(changestunumber, changename, changeage, changesex, changescore);
break;
case 5:
System.out.print("请输入你要查询的学生学号:");
String selnumber = scanner.nextLine();
selectStudent(selnumber);
break;
default:
System.out.println("该选项不存在,请你重新输入!");
}
} while (true);
}
}
</details>
----------------------Moonbeamsc.