我的for循环中看不到第二类的方法引用

时间:2019-05-23 19:31:10

标签: java for-loop method-call

我正在尝试完成一个for循环,在该循环中我引用了UAClass类的getCredits()方法。我继续收到一条错误消息,指出找不到该符号,并且指向第68和73行中代码的“ .getNumCredits”部分。对于Java来说我是相对较新的人,但是我确保检查了我的拼写等。任何帮助将不胜感激!

我检查了拼写和大小写,以确保for循环中的引用与另一个类中的方法匹配。

public class Student{
    private String firstName;
    private String lastName;
    private String departmentIn;
    private int yearGraduation;
    private double gpa;
    private double[] gradeReceived = {4.0, 3.0, 3.0, 2.0, 4.0};
    private String[] classList = {"history", "biology", "physics", "english", "finance"};
    private static double totalClassPoints;
    private static int totalCredits;

    public Student(String firstName, String lastName, String departmentIn, int yearGraduation) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.departmentIn = departmentIn;
        this.yearGraduation = yearGraduation;
    }

     UAClass history = new UAClass("Bob", "Smith", "Spring", 3);
     UAClass biology = new UAClass("Reggie", "Jones", "Fall", 3);
     UAClass physics = new UAClass("Carl", "Grimes", "Fall", 4);
     UAClass english = new UAClass("Rebecca", "Johnson", "Spring", 3);
     UAClass finance = new UAClass("Rachel", "Bomber", "Spring", 3);

    public void setStudentFirstName(String StudentFirstName){
        StudentFirstName = firstName;
    }

    public void setStudentLastName(String StudentLastName){
        StudentLastName = lastName;
    }

    public void setDepartmentIn(String DepartmentIn){
        DepartmentIn = departmentIn;
    }

    public void setYearGraduation(int YearGraduation){
        YearGraduation = yearGraduation;
    }

    public String getStudentFirstName(){
        return firstName;
    }

    public String getStudentLastName(){
        return lastName;
    }

    public String getDepartmentIn(){
        return departmentIn;
    }

    public int getYearGraduation(){
        return yearGraduation;
    }

    public double getGPA(){
        return gpa;
    }

    public void calcGPA(){
        for(int i=0; i < 5; i++){

            totalClassPoints += (gradeReceived[i] * classList[i].getNumCredits()); 
        }

            for(int i=0; i < 5; i++){

                totalCredits += classList[i].getNumCredits();
        }

        gpa = totalClassPoints/totalCredits;

        System.out.println("\nFirst Name: " + firstName + "\nLast Name: " + lastName + "\nDepartmentIn: " + departmentIn + "\nGraduation Year: " + yearGraduation + "\nGPA: " + gpa);

        }
        public static void main(String []args){

        Student st1 = new Student("Matt", "Watson", "MIS", 2019);

        st1.calcGPA();
        System.out.println();
   }
     class UAClass{

         public String teacherFirstName;
         public String teacherLastName;
         public String semesterOffered;
         public int numCredits;

         public UAClass(String teacherFirstName, String teacherLastName, String semesterOffered, int numCredits){
             this.teacherFirstName = teacherFirstName;
             this.teacherLastName = teacherLastName;
             this.semesterOffered = semesterOffered;
             this.numCredits = numCredits;
         }

         public void setTeacherFirstName(String TeacherFirstName){
             TeacherFirstName = teacherFirstName;
         }
         public void setTeacherLastName(String TeacherLastName){
             TeacherLastName = teacherLastName;
         }
         public void setSemesterOffered(String SemesterOffered){
             SemesterOffered = semesterOffered;
         }
         public void setNumCredits(int NumCredits){
             NumCredits = numCredits;
         }
         public String getTeacherFirstName(){
             return teacherFirstName;
         }
         public String getTeacherLastName(){
             return teacherLastName;
         }
         public String getSemesterOffered(){
             return semesterOffered;
         }
         public int getNumCredits(){
             return numCredits;
         }

1 个答案:

答案 0 :(得分:0)

Nitika在评论中的回答:

  

这里的classList [i]是String,要调用UAClass类的方法,您需要该类的引用。