java—向数组中添加一个项,只要该项不在数组中且数组未满

f45qwnt8  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(412)

关闭。这个问题需要更加突出重点。它目前不接受答案。
**想改进这个问题吗?**通过编辑这篇文章更新这个问题,使它只关注一个问题。

上个月关门了。
改进这个问题
我的要求是为学生和教员进行课程注册。我的问题是,只要学生还没有在数组中,并且数组还没有满,就将学生添加到数组中。我已经在这工作了一整天,所以它看起来破损,任何帮助或一点在正确的方向是感激的。谢谢您。
这是我的课程java文件:

package registration;

public class Course{

    private String department;
    private String courseName;
    private int maxSize;
    private int currentEnrollmentNumber;
    private Student roster[];
    private Faculty facultyInstructor;

    public Course(String department, String courseName, int maxSize) {
        this.department = department;
        this.courseName = courseName;
        this.setMaxSize(maxSize);
        setCurrentEnrollmentNumber(0);
        roster = new Student[maxSize];
    }

    void assignInstructor(Faculty f) {
        if(f.getDepartment().equals(department)) {
            facultyInstructor = f;
            System.out.println(f.getName() + " has been assigned to teach this course");
        }else {
            System.out.println(f.getName() + " cannot be assigned to this course. Only faculties from " + department + " department can be assigned to teach this course.");
        }
    }

    void addStudent(Student s) {
        do {
            roster[currentEnrollmentNumber] = s;
            System.out.println(s.getName() + " has been added to the course");
            for (int i = currentEnrollmentNumber; i < maxSize; i++) {
            }

        } while (!s.equals(roster[currentEnrollmentNumber]));

//      if (currentEnrollmentNumber < maxSize && )) {
//              roster[currentEnrollmentNumber] = s;
//              System.out.println(s.getName() + " has been added to the course");
//              currentEnrollmentNumber++;
    }// System.out.println(s.getName() + " is already registered");

这是学生java文件:

package registration;

public class Student extends Person {
        private String major;
        private double gpa;

        Student(){

        }

        Student(String name, int age, String email, String major, double gpa){
            super(name, age, email);
            this.major = major; 
            this.gpa = gpa;
        }

        void registerForCourse(Course c) {
            if(c.getMaxSize() <= c.getCurrentEnrollmentNumber()) {
                System.out.println("Class is at maximum capacity");
            }
            for(int i = 0; i < c.getCurrentEnrollmentNumber(); i++) {
                if(c.getRoster()[i].equals(this)) {
                    return;
                }
            }
            c.getRoster()[c.getCurrentEnrollmentNumber()] = this;
            c.setCurrentEnrollmentNumber(c.getCurrentEnrollmentNumber() + 1);
        } 

        public boolean equals(Object otherObject) {
            if(otherObject == null) {
                return false;
            } else {
                if(getClass() == otherObject.getClass()) {
                    Student otherStudent = (Student)otherObject;
                    return (getName().equals(otherStudent.getName()) && getAge() == otherStudent.getAge() && getEmail().equals(otherStudent.getEmail()));
                } else {
                    return false;
                }
            }
        }

        public boolean equals(Student otherStudent) {
            return super.equals(otherStudent) && getMajor().equals(otherStudent.major) && getGpa() == otherStudent.gpa;
        }
hgqdbh6s

hgqdbh6s1#

欢迎来到stackoverflow!你现在的方法确实让人感到奇怪和困惑,让我们来整理一下。
首先,执行if循环,遍历所有当前学生,查看该学生是否已经注册。如果是,请打印相应的消息并立即调用 return 关闭方法。不需要处理其他任何事情。
如果学生尚未注册,请找出是否有空间添加他们。
如果没有更多的空间,请打印相应的消息并 return .
如果此学生有空间,请继续添加:)
考虑将其中的一些方法分解为单独的方法,例如boolean isstudentalreadyenrolled(student-student)、void enrollment(student-student)。将代码分解成更小的步骤可以帮助您提高代码的可读性、可测试性和可重用性。

相关问题