"grade" 无法解析为变量

"grade" cannot be resolved to a variable

我有这个练习,问题是如何解决错误“成绩”无法解析为变量,而不在 class 老师中声明它。我想这是我的代码中唯一的错误。对我来说很明显为什么在我的输出中只有等级变量没有分配,但我不知道如何解决它。

public class App {
    public static void main(String[] args) throws Exception {

        Student studentOne = new Student("FraPedu");
        Student studentTwo = new Student("FraIla");
        Teacher teacherOne = new Teacher();
        
        teacherOne.teacherName = "Tiziana";

        teacherOne.assignGrade(studentOne,10);
        teacherOne.assignGrade(studentTwo,10);
        studentOne.getStudentDetails();
        studentTwo.getStudentDetails();
    }
}

public class Student {

    public String name;
    public int grade;

    public Student(String studentName) {

        System.out.println("Student object has been created succesfully!");
        name = studentName;
    }

    public void getStudentDetails() {

        System.out.println("Student name and grade: " + name + " " + grade);        
    }
}

public class Teacher {

    public String teacherName;

    public Teacher() {

        System.out.println("Teacher object has been created succesfully!");
    }

    public void assignGrade(Student alum, int finalGrade) {

        grade = finalGrade; 
    }
}

您需要将成绩分配给要传递给 assignGrade 方法的 Student 对象。


public class Teacher {

    public String teacherName;

    public Teacher() {

        System.out.println("Teacher object has been created succesfully!");
    }

    public void assignGrade(Student alum, int finalGrade) {

        alum.grade = finalGrade; // << this is the line I changed
    }
}