“等级”无法解析为变量
我有这个练习,问题是如何解决错误“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将成绩分配给要传递给
assignGrade
方法的Student
对象。You need to assign the grade to the
Student
object you are passing to theassignGrade
method.