“等级”无法解析为变量

发布于 2025-01-10 16:15:11 字数 1253 浏览 0 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南渊 2025-01-17 16:15:11

您需要将成绩分配给要传递给 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
    }
}

You need to assign the grade to the Student object you are passing to the assignGrade method.


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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文