简体   繁体   English

“等级”无法解析为变量

[英]"grade" cannot be resolved to a variable

I have this exercise and the question is how can I solve the error "grade" cannot be resolved to a variable, without declare it in the class teacher.我有这个练习,问题是如何解决错误“成绩”无法解析为变量,而无需在 class 老师中声明它。 I suppose is the only error in my code.我想这是我的代码中唯一的错误。 It is evident for me WHY then in my output only the grade variable is not assigned, but I don't know HOW to solve it.对我来说很明显为什么然后在我的 output 中只有等级变量没有分配,但我不知道如何解决它。

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

You need to assign the grade to the Student object you are passing to the assignGrade method.您需要将成绩分配给您传递给assignGrade方法的Student object。


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM