2024/9/20

今天进行了Java测试成功通过。
import java.util.Scanner;
import java.util.Arrays;
import java.util.*;
class Student {
private String stunumber;//学号
private String name;//姓名
private int age;//年龄
private boolean sex;//男生为false,女生为true
private double score;//成绩

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;
}

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 isSex() {
    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;
private int size;//已记录的学生数量

public StudentManager() {
    students = new Student[5];//最多记录5名学生
    size = 0;
}//构造函数

public void showStudent() {
    for (int i = 0; i < size; i++) {
        Student student = students[i];
        System.out.println("学号:" + student.getStunumber() + ", 姓名:" + student.getName() + ", 年龄:" + student.getAge() + ", 性别:" + (student.isSex()? "女" : "男") + ", 成绩:" + student.getScore());
    }
}//方法打印输出所有的学生信息;

public void addStudent(String stunumber, String name, int age, boolean sex, double score) {
    for (int i = 0; i < size; i++) {
        if (students[i].getStunumber().equals(stunumber)) {
            System.out.println("学号重复,禁止增加新学生信息。");
            return;
        }//判断学号是否重复
    }
    if (size < students.length/*存储学生对象的数组的长度*/) {
        students[size++] = new Student(stunumber, name, age, sex, score);//储存新数据
        System.out.println("添加学生信息成功。");
    } else {
        System.out.println("存储空间已满,无法添加新学生信息。");
    }
}//增加一名新学生的信息

public void deleteStudent(String stunumber) {
    for (int i = 0; i < size; i++) {
        if (students[i].getStunumber().equals(stunumber)) {
            for (int j = i; j < size - 1; j++) {
                students[j] = students[j + 1];
            }
            students[size - 1] = null;
            size--;
             return;
        }
    }
    System.out.println("未找到该学号的学生,无法删除。");
}//删除一名学生信息

public void updateStudent(String stunumber, String name, int age, boolean sex, double score) {
    for (int i = 0; i < size; 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("未找到该学号的学生,无法修改。");
}//修改学生信息

public void selectStudent(String stunumber) {
    for (int i = 0; i < size; i++) {
        if (students[i].getStunumber().equals(stunumber)) {
            Student student = students[i];
            System.out.println("学号:" + student.getStunumber() + ", 姓名:" + student.getName() + ", 年龄:" + student.getAge() + ", 性别:" + (student.isSex()? "女" : "男") + ", 成绩:" + student.getScore());
            return;
        }
    }
    System.out.println("未找到该学号的学生。");
}//查询并且输出该名学生的全部信息

public static void main(String[] args) {
    StudentManager manager = new StudentManager();
    Scanner scanner = new Scanner(System.in);

    while (true) {
        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("****************************************************************");
        System.out.print("请输入功能选项:");
        int choice = scanner.nextInt();

        switch (choice) {
            case 1:
                manager.showStudent();
                break;
            case 2:
                System.out.print("请输入学号:");
                String stunumber2 = scanner.next();
                System.out.print("请输入姓名:");
                String name2 = scanner.next();
                System.out.print("请输入年龄:");
                int age2 = scanner.nextInt();
                System.out.print("请输入性别(男为 false,女为 true):");
                boolean sex2 = scanner.nextBoolean();
                System.out.print("请输入成绩:");
                double score2 = scanner.nextDouble();
                manager.addStudent(stunumber2, name2, age2, sex2, score2);
                break;
            case 3:
                System.out.print("请输入要删除的学生学号:");
                String stunumber3 = scanner.next();
                manager.deleteStudent(stunumber3);
                break;
            case 4:
                System.out.print("请输入要修改的学生学号:");
                String stunumber4 = scanner.next();
                System.out.print("请输入新姓名:");
                String name4 = scanner.next();
                System.out.print("请输入新年龄:");
                int age4 = scanner.nextInt();
                System.out.print("请输入新性别(男为 false,女为 true):");
                boolean sex4 = scanner.nextBoolean();
                System.out.print("请输入新成绩:");
                double score4 = scanner.nextDouble();
                manager.updateStudent(stunumber4, name4, age4, sex4, score4);
                break;
            case 5:
                System.out.print("请输入要查询的学生学号:");
                String stunumber5 = scanner.next();
                manager.selectStudent(stunumber5);
                break;
            default:
                System.out.println("输入错误,请重新输入。");
        }
    }
}

}

posted on 2024-09-20 23:22  不耻  阅读(8)  评论(0编辑  收藏  举报

导航