gpt4 book ai didi

java - 在我的 for 循环中没有看到第二类的方法引用

转载 作者:行者123 更新时间:2023-12-02 05:18:06 25 4
gpt4 key购买 nike

我正在尝试完成一个 for 循环,其中引用 UAClass 类中的 getCredits() 方法。我继续收到一个错误,指出找不到该符号,它指向第 68 行和第 73 行代码的“.getNumCredits”部分。我对 java 相对较新,但我确保检查了拼写等。任何帮助将不胜感激!

我检查了拼写和大小写,以确保 for 循环中的引用与其他类中的方法匹配。

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.

关于java - 在我的 for 循环中没有看到第二类的方法引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56281843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com