【问题标题】:arrayList of type Student.Graduate has incorrect typesStudent.Graduate 类型的 arrayList 类型不正确
【发布时间】:2013-12-01 01:26:36
【问题描述】:

行号为here的代码

package project5_test2;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 *
 * @author David
 */
public class Project5_test2 {
    public static Object Student;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner(System.in);
        // initialize an arary that holds N objects of type Student
        int end = 1;
        ArrayList<Student> studentList = new ArrayList<>();

        // read in the data
        for (int y = 0; y < end;) {
            String nextMove = nextMove();
            for (int i = 0; i < end;) {
                if ("add".equalsIgnoreCase(nextMove)) {
                    System.out.println("Student Input: ");
                    String firstName = getFirstName();
                    String lastName = getLastName();
                    int uid = getUid();
                    String stringUid = Integer.toString(uid);
                    StudentType type = inputStudentType();  
                    if (type == StudentType.UNDECLARED) {
                        ClassStanding standing = inputClassStanding();
                        studentList.add(new Student(firstName, lastName, uid, type, standing ));
                    }
                    if (type == StudentType.UNDERGRADUATE) {
                        ClassStanding standing = inputClassStanding();
                        Major major = inputMajor();
                        double overallGpa = getOverallGPA();
                        String overallGpaDouble = Double.toString(overallGpa);
                        double majorGpa = getOverallGPA();
                        String majorGpaDouble = Double.toString(majorGpa);
                        studentList.add(new Student(firstName, lastName, uid, type, standing));

                    }
                    if (type == StudentType.GRADUATE) {
                        boolean thesis = getThesisStatus();
                        String thesisString = Boolean.toString(thesis);
                        ClassStanding studyType = getStudy();
                        String profName = getProfName();
                        studentList.add(new Student.Graduate( thesis, studyType, profName));
                    }
                } i++;
            }

            if ("remove".equalsIgnoreCase(nextMove)) {
                    System.out.println("derp");
                }

            if ("save".equalsIgnoreCase(nextMove)) {
                for (int i = 0; i < studentList.size(); i++) {
                    System.out.println(studentList.get(i));

                }
                y++;
            }
        }
    }

    enum ClassStanding {

        FRESHMAN, SOPHOMORE, JUNIOR, SENIOR, UNKNOWN,
        MASTERS_STUDIES, PHD_STUDIES, NO_STANDING
    };

    enum StudentType {

        UNDERGRADUATE, GRADUATE, UNDECLARED
    };

    enum Major {

        CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN
    };

    public static class Student {

        public String firstName;     // first name
        public String lastName;
        public double uid;      // last name
        public StudentType type;
        public ClassStanding standing;

        public Student(Student orig) {
        }

        // construct a new student with given fields
        public Student(String firstName, String lastName, Integer newUid, StudentType type, ClassStanding standing) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.uid = newUid;
            this.type = type;
            this.standing = standing;
        }


        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public String getLastName() {
            return lastName;
        }
        //set type

        public void setType(StudentType type) {
            this.type = type;
        }
        //return type

        public StudentType getType() {
            return type;
        }

        public void setStanding(ClassStanding standing) {
            this.standing = standing;
        }
        //return type

        public ClassStanding getStanding() {
            return standing;
        }

        // return a string representation of the invoking object
        public String toString() {
            return firstName + " " + lastName + " " + uid + " " + type + " " + standing;
        }

        public static class Graduate extends Student {

            public boolean thesis;
            public ClassStanding study;
            public String profName;

            public Graduate(Student orig, boolean isThesis, ClassStanding study, String profName) {
                super(orig);
            }

            public boolean getThesis() {
                return thesis;
            }

            public void setThesis(Boolean thesis) {
                this.thesis = thesis;
            }

            public ClassStanding getStudy() {
                return study;
            }

            public void setStudy(ClassStanding study) {
                this.study = study;
            }

            public String getProfName() {
                return profName;
            }

            public void setProfName(String profName) {
                this.profName = profName;
            }

            public String toString() {
                return thesis + " " + study + " " + profName;
            }
        }

        public static class UnderGraduate extends Student {

            public Major major;
            public Double GPA;

            public UnderGraduate(Student orig, Major major, Double GPA) {
                super(orig);
            }

            public void setMajor(Major major) {
                this.major = major;
            }
            //return type

            public Major getmMajor() {
                return major;
            }

            public void setOverallGPA(Integer uid) {
                this.uid = uid;
            }

            public double getOverallGPA() {
                return uid;
            }

            public String toString() {
                return major + " " + uid ;
            }
        }
    }

    public static String getFirstName() {
        Scanner input = new Scanner(System.in);
        String firstName;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter the students first name: ");
                firstName = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a string please enter another first name!");
                input.next();
                continue;
            }
        }
        return firstName;
    }

    public static String getLastName() {
        Scanner input = new Scanner(System.in);
        String lastName;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter a students last name: ");
                lastName = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a string please enter another last name!");
                input.next();
                continue;
            }
        }
        return lastName;
    }

    public static Integer getUid() {
        Scanner input = new Scanner(System.in);
        int uid;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter a uid number: ");
                uid = input.nextInt();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a number or an integer!");
                input.next();
                continue;
            }
        }
        return uid;
    }

    public static StudentType inputStudentType() {
        Scanner input = new Scanner(System.in);
        String type;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Is the student a graduate, undergraduate, or, undeclared: ");
                type = input.next();

                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("False input. Is the student a graduate, undergraduate, or, undeclared: ");
                type = input.next();
                continue;
            }
        }
        if ("undergraduate".equalsIgnoreCase(type)) {
            StudentType status = StudentType.UNDERGRADUATE;
            return status;
        }
        if ("graduate".equalsIgnoreCase(type)) {
            StudentType status = StudentType.GRADUATE;
            return status;
        }
        if ("undeclared".equalsIgnoreCase(type)) {
            StudentType status = StudentType.UNDECLARED;
            return status;
        }
        return null;

    }

    public static String nextMove() {
        Scanner input = new Scanner(System.in);
        String nextMove;
        while (true) {
            // prompt the user to enter if there are more characers
            try {
                System.out.print("What now? Add, Sort, Remove, Save: ");
                nextMove = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("More input? true or false(type true or false): ");
                input.next();
                continue;
            }
        }
        return nextMove;
    }

    public static ClassStanding inputClassStanding() {
        Scanner input = new Scanner(System.in);
        String type;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Enter the students class standing:  ");
                type = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("False input. Input the students class standing: ");
                type = input.next();
                continue;
            }
        }
        if ("Freshman".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.FRESHMAN;

            return standing;
        }
        if ("sophomore".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.SOPHOMORE;

            return standing;
        }
        if ("junior".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.JUNIOR;

            return standing;
        }
        if ("senior".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.SENIOR;
            return standing;
        }
        if ("unknown".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.UNKNOWN;
            return standing;
        }
        return null;

    }

    public static Major inputMajor() {
        Scanner input = new Scanner(System.in);
        String major;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Enter the students major (CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN):  ");
                major = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("False input. Enter the students major (CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN): ");
                major = input.next();
                continue;
            }
        }
        if ("cs".equalsIgnoreCase(major)) {
            Major finalMajor = Major.CS;
            return finalMajor;
        }
        if ("ceg".equalsIgnoreCase(major)) {
            Major finalMajor = Major.CEG;
            return finalMajor;
        }
        if ("ee".equalsIgnoreCase(major)) {
            Major finalMajor = Major.EE;
            return finalMajor;
        }
        if ("ise".equalsIgnoreCase(major)) {
            Major finalMajor = Major.ISE;
            return finalMajor;
        }
        if ("bme".equalsIgnoreCase(major)) {
            Major finalMajor = Major.BME;
            return finalMajor;
        }
        if ("ME".equalsIgnoreCase(major)) {
            Major finalMajor = Major.ME;
            return finalMajor;
        }
        if ("met".equalsIgnoreCase(major)) {
            Major finalMajor = Major.MET;
            return finalMajor;
        }
        if ("unknown".equalsIgnoreCase(major)) {
            Major finalMajor = Major.UNKNOWN;
            return finalMajor;
        }
        return null;

    }

    public static Double getOverallGPA() {
        Scanner input = new Scanner(System.in);
        double overallGpa;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter the students overall GPA: ");
                overallGpa = input.nextDouble();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a number or an integer! Please enter the students overall GPA:");
                input.next();
                continue;
            }
        }
        return overallGpa;
    }

    public static Double getMajorlGPA() {
        Scanner input = new Scanner(System.in);
        double majorGpa;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter the students major GPA: ");
                majorGpa = input.nextDouble();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a number or an integer! Please enter the students major GPA:");
                input.next();
                continue;
            }
        }
        return majorGpa;
    }

    public static boolean getThesisStatus() {
        Boolean thesis;
        Scanner input = new Scanner(System.in);
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Enter TRUE for having a thesis option else FALSE: ");
                thesis = input.nextBoolean();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a boolean response. True or false, is there a thesis: ");
                input.next();
                continue;
            }
        }
        return thesis;
    }

    public static ClassStanding getStudy() {
        String study = null;
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < 1;) {
            while (true) {
                //prompt the user to enter the popularity number for each character
                try {
                    System.out.print("Enter Masters for Master Studies or Phd for Phd studies: ");
                    study = input.next();
                    break;
                    //error handling
                } catch (InputMismatchException e) {
                    System.out.println("Not a valid response. Enter Masters for Master Studies or Phd for Phd studies: ");
                    input.next();
                    continue;
                }
            }
            if ("masters".equalsIgnoreCase(study)) {
                ClassStanding standing = ClassStanding.MASTERS_STUDIES;
                return standing;
            }
            if ("phd".equalsIgnoreCase(study)) {
                ClassStanding standing = ClassStanding.PHD_STUDIES;
                return standing;
            }
        }
        return null;
    }

    public static String getProfName() {
        String name;
        Scanner input = new Scanner(System.in);
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Enter the name of the major professor: ");
                name = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a valid response. Enter the name of the major professor: ");
                input.next();
                continue;
            }
        }
        return name;
    }   
}

问题目前显示在第 54 行,同样的错误将出现在第 46 行。

写入 ArrayList studentList 时,我需要包含一个 Student 类型的变量。 这个问题可能出现在我的子类的构造函数中,但我对子类并不完全熟悉。

【问题讨论】:

  • 如果你没有在里面做任何事情,为什么你需要public Student(Student orig) { }构造函数?它应该是复制构造函数吗?
  • 我不确定,但我相信。行 studentList.add(new Student.Graduate( xxxxxxxxxx , thesis, studyType, profName)); xxxxxx 缺失,需要是学生类型。我该怎么做。它可能与未使用的构造函数有关。
  • 我一直讨厌方法太长,以至于您必须滚动才能看到它们的完整主体(许多其他人就像我一样)。将这么长的方法分解成单独的方法,正确命名每个方法,这样您的代码将更具可读性,并且每个人都可以更轻松、更多地帮助您。

标签: java class inheritance arraylist subclass


【解决方案1】:

Student.Graduate 接收一个 Student 作为构造函数中的第一个参数 - 您忘记发送它,因此编译错误。

如果您将第 54 行替换为:

studentList.add(new Student.Graduate(
                new Student(firstName, lastName, uid, type,null ), 
                thesis, studyType, profName));

你不会得到那个编译错误。
也就是说 - 代码中还有其他问题,请参阅下面的固定代码:

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Project5_test2 {
    public static Object Student;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner(System.in);
        // initialize an arary that holds N objects of type Student
        int end = 1;
        ArrayList<Student> studentList = new ArrayList<Student>();

        // read in the data
        for (int y = 0; y < end;) {
            String nextMove = nextMove();
            for (int i = 0; i < end;) {
                if ("add".equalsIgnoreCase(nextMove)) {
                    System.out.println("Student Input: ");
                    String firstName = getFirstName();
                    String lastName = getLastName();
                    int uid = getUid();
                    String stringUid = Integer.toString(uid);
                    StudentType type = inputStudentType();
                    ClassStanding standing = inputClassStanding();
                    Student base = new Student(firstName, lastName, uid, type, standing);
                    if (type == StudentType.UNDECLARED) {
                        studentList.add(base);
                    }
                    else if (type == StudentType.UNDERGRADUATE) {
                        Major major = inputMajor();
                        double overallGpa = getOverallGPA();
                        String overallGpaDouble = Double.toString(overallGpa);
                        double majorGpa = getOverallGPA();
                        String majorGpaDouble = Double.toString(majorGpa);
                        studentList.add(base);
                    }
                    else if (type == StudentType.GRADUATE) {
                        boolean thesis = getThesisStatus();
                        //String thesisString = Boolean.toString(thesis);
                        ClassStanding studyType = getStudy();
                        String profName = getProfName();
                        studentList.add(new Student.Graduate(base, thesis, studyType, profName));
                    }
                } i++;
            }

            if ("remove".equalsIgnoreCase(nextMove)) {
                System.out.println("derp");
            }

            if ("save".equalsIgnoreCase(nextMove)) {
                for (int i = 0; i < studentList.size(); i++) {
                    System.out.println(studentList.get(i));
                }
                y++;
            }
        }
    }

    enum ClassStanding {

        FRESHMAN, SOPHOMORE, JUNIOR, SENIOR, UNKNOWN,
        MASTERS_STUDIES, PHD_STUDIES, NO_STANDING
    };

    enum StudentType {

        UNDERGRADUATE, GRADUATE, UNDECLARED
    };

    enum Major {

        CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN
    };

    public static class Student {

        public String firstName;     // first name
        public String lastName;
        public double uid;      // last name
        public StudentType type;
        public ClassStanding standing;

        public Student(Student orig) {
            this.firstName = orig.firstName;
            this.lastName = orig.lastName;
            this.uid = orig.uid;
            this.type = orig.type;
            this.standing = orig.standing;
        }

        // construct a new student with given fields
        public Student(String firstName, String lastName, Integer newUid, StudentType type, ClassStanding standing) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.uid = newUid;
            this.type = type;
            this.standing = standing;
        }


        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public String getLastName() {
            return lastName;
        }
        //set type

        public void setType(StudentType type) {
            this.type = type;
        }
        //return type

        public StudentType getType() {
            return type;
        }

        public void setStanding(ClassStanding standing) {
            this.standing = standing;
        }
        //return type

        public ClassStanding getStanding() {
            return standing;
        }

        // return a string representation of the invoking object
        public String toString() {
            return firstName + " " + lastName + " " + uid + " " + type + " " + standing;
        }

        public static class Graduate extends Student {

            public boolean thesis;
            public ClassStanding study;
            public String profName;

            public Graduate(Student orig, boolean isThesis, ClassStanding study, String profName) {
                super(orig);
                thesis = isThesis;
                this.study = study;
                this.profName = profName;
            }

            public boolean getThesis() {
                return thesis;
            }

            public void setThesis(Boolean thesis) {
                this.thesis = thesis;
            }

            public ClassStanding getStudy() {
                return study;
            }

            public void setStudy(ClassStanding study) {
                this.study = study;
            }

            public String getProfName() {
                return profName;
            }

            public void setProfName(String profName) {
                this.profName = profName;
            }

            public String toString() {
                return super.toString() + thesis + " " + study + " " + profName;
            }
        }

        public static class UnderGraduate extends Student {

            public Major major;
            public Double GPA;

            public UnderGraduate(Student orig, Major major, Double GPA) {
                super(orig);
            }

            public void setMajor(Major major) {
                this.major = major;
            }
            //return type

            public Major getmMajor() {
                return major;
            }

            public void setOverallGPA(Integer uid) {
                this.uid = uid;
            }

            public double getOverallGPA() {
                return uid;
            }

            public String toString() {
                return major + " " + uid ;
            }
        }
    }

    public static String getFirstName() {
        Scanner input = new Scanner(System.in);
        String firstName;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter the students first name: ");
                firstName = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a string please enter another first name!");
                input.next();
                continue;
            }
        }
        return firstName;
    }

    public static String getLastName() {
        Scanner input = new Scanner(System.in);
        String lastName;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter a students last name: ");
                lastName = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a string please enter another last name!");
                input.next();
                continue;
            }
        }
        return lastName;
    }

    public static Integer getUid() {
        Scanner input = new Scanner(System.in);
        int uid;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter a uid number: ");
                uid = input.nextInt();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a number or an integer!");
                input.next();
                continue;
            }
        }
        return uid;
    }

    public static StudentType inputStudentType() {
        Scanner input = new Scanner(System.in);
        String type;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Is the student a graduate, undergraduate, or, undeclared: ");
                type = input.next();

                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("False input. Is the student a graduate, undergraduate, or, undeclared: ");
                type = input.next();
                continue;
            }
        }
        if ("undergraduate".equalsIgnoreCase(type)) {
            StudentType status = StudentType.UNDERGRADUATE;
            return status;
        }
        if ("graduate".equalsIgnoreCase(type)) {
            StudentType status = StudentType.GRADUATE;
            return status;
        }
        if ("undeclared".equalsIgnoreCase(type)) {
            StudentType status = StudentType.UNDECLARED;
            return status;
        }
        return null;

    }

    public static String nextMove() {
        Scanner input = new Scanner(System.in);
        String nextMove;
        while (true) {
            // prompt the user to enter if there are more characers
            try {
                System.out.print("What now? Add, Sort, Remove, Save: ");
                nextMove = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("More input? true or false(type true or false): ");
                input.next();
                continue;
            }
        }
        return nextMove;
    }

    public static ClassStanding inputClassStanding() {
        Scanner input = new Scanner(System.in);
        String type;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Enter the students class standing:  ");
                type = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("False input. Input the students class standing: ");
                type = input.next();
                continue;
            }
        }
        if ("Freshman".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.FRESHMAN;

            return standing;
        }
        if ("sophomore".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.SOPHOMORE;

            return standing;
        }
        if ("junior".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.JUNIOR;

            return standing;
        }
        if ("senior".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.SENIOR;
            return standing;
        }
        if ("unknown".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.UNKNOWN;
            return standing;
        }
        return null;

    }

    public static Major inputMajor() {
        Scanner input = new Scanner(System.in);
        String major;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Enter the students major (CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN):  ");
                major = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("False input. Enter the students major (CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN): ");
                major = input.next();
                continue;
            }
        }
        if ("cs".equalsIgnoreCase(major)) {
            Major finalMajor = Major.CS;
            return finalMajor;
        }
        if ("ceg".equalsIgnoreCase(major)) {
            Major finalMajor = Major.CEG;
            return finalMajor;
        }
        if ("ee".equalsIgnoreCase(major)) {
            Major finalMajor = Major.EE;
            return finalMajor;
        }
        if ("ise".equalsIgnoreCase(major)) {
            Major finalMajor = Major.ISE;
            return finalMajor;
        }
        if ("bme".equalsIgnoreCase(major)) {
            Major finalMajor = Major.BME;
            return finalMajor;
        }
        if ("ME".equalsIgnoreCase(major)) {
            Major finalMajor = Major.ME;
            return finalMajor;
        }
        if ("met".equalsIgnoreCase(major)) {
            Major finalMajor = Major.MET;
            return finalMajor;
        }
        if ("unknown".equalsIgnoreCase(major)) {
            Major finalMajor = Major.UNKNOWN;
            return finalMajor;
        }
        return null;

    }

    public static Double getOverallGPA() {
        Scanner input = new Scanner(System.in);
        double overallGpa;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter the students overall GPA: ");
                overallGpa = input.nextDouble();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a number or an integer! Please enter the students overall GPA:");
                input.next();
                continue;
            }
        }
        return overallGpa;
    }

    public static Double getMajorlGPA() {
        Scanner input = new Scanner(System.in);
        double majorGpa;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter the students major GPA: ");
                majorGpa = input.nextDouble();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a number or an integer! Please enter the students major GPA:");
                input.next();
                continue;
            }
        }
        return majorGpa;
    }

    public static boolean getThesisStatus() {
        Boolean thesis;
        Scanner input = new Scanner(System.in);
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Enter TRUE for having a thesis option else FALSE: ");
                thesis = input.nextBoolean();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a boolean response. True or false, is there a thesis: ");
                input.next();
                continue;
            }
        }
        return thesis;
    }

    public static ClassStanding getStudy() {
        String study = null;
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < 1;) {
            while (true) {
                //prompt the user to enter the popularity number for each character
                try {
                    System.out.print("Enter Masters for Master Studies or Phd for Phd studies: ");
                    study = input.next();
                    break;
                    //error handling
                } catch (InputMismatchException e) {
                    System.out.println("Not a valid response. Enter Masters for Master Studies or Phd for Phd studies: ");
                    input.next();
                    continue;
                }
            }
            if ("masters".equalsIgnoreCase(study)) {
                ClassStanding standing = ClassStanding.MASTERS_STUDIES;
                return standing;
            }
            if ("phd".equalsIgnoreCase(study)) {
                ClassStanding standing = ClassStanding.PHD_STUDIES;
                return standing;
            }
        }
        return null;
    }

    public static String getProfName() {
        String name;
        Scanner input = new Scanner(System.in);
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Enter the name of the major professor: ");
                name = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a valid response. Enter the name of the major professor: ");
                input.next();
                continue;
            }
        }
        return name;
    }
}

更新

如果不希望研究生有班级排名,可以设置为null,修改如下:

if (type == StudentType.GRADUATE) {
    boolean thesis = getThesisStatus();
    String thesisString = Boolean.toString(thesis);
    ClassStanding studyType = null;//getStudy(); <-- change this line
    String profName = getProfName();
    studentList.add(new Student.Graduate( thesis, studyType, profName));
}

【讨论】:

  • 是的,这似乎工作得很好,但现在我认为我的子类的问题真的开始显现了。当我为研究生打印数组列表时,所有字段都为空
  • 是的,那是因为Student(Student orig){} 构造函数都是空的,而且毕业生构造函数没有保存信息。修复两个构造函数,你就可以开始了。查看更新(代码修复)。
  • 是的,非常感谢!但是对于最后一个问题,我不知道为什么当我运行它并宣布一名学生为毕业生时,它会问我学生的班级排名。等价于 ClassStanding studyType = getStudy();对于毕业生
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
  • 1970-01-01
  • 2018-05-21
  • 2020-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多