在我的 for 循环中没有看到来自第二个 class 的方法引用
Method reference from second class isn't being seen in my for loop
我正在尝试完成一个 for 循环,其中我引用了 UAClass class 中的方法 getCredits()。我继续收到一个错误,指出找不到该符号,它指向第 68 行和第 73 行代码的“.getNumCredits”部分。我对 java 比较陌生,但我确保检查了我的拼写等。如有任何帮助,我们将不胜感激!
我检查了拼写和大小写以确保 for 循环中的引用与另一个 class 中的方法匹配。
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;
}
Nitika 在评论中的回答:
Here classList[i] is String and to call methods of UAClass class, you need a reference of that class.
我正在尝试完成一个 for 循环,其中我引用了 UAClass class 中的方法 getCredits()。我继续收到一个错误,指出找不到该符号,它指向第 68 行和第 73 行代码的“.getNumCredits”部分。我对 java 比较陌生,但我确保检查了我的拼写等。如有任何帮助,我们将不胜感激!
我检查了拼写和大小写以确保 for 循环中的引用与另一个 class 中的方法匹配。
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;
}
Nitika 在评论中的回答:
Here classList[i] is String and to call methods of UAClass class, you need a reference of that class.