内部 if 语句被完全跳过,当我单步执行代码时它永远不会进入 updateGrade。它只是完全跳过了 if 。为什么?
继承人有问题的方法:
public void updateCourse(String courseID, String newLetterGrade){
for(int i=0; i<classesTaken.size(); i++){
if(classesTaken.contains(courseID)){
classesTaken.get(i).updateGrade(newLetterGrade);
}
}
}
根据我的调试器,当 i = 5 和我的 arrayList = 6 时,一旦我达到 6,它会立即无缘无故地跳出循环。我不确定出了什么问题,因为在 make 或运行时没有显示错误。
这是在 classesTaken 中找到 courseID 的内容时调用的方法:
public void updateGrade(String newGrade){
letterGrade=newGrade;
compute(newGrade);
我的测试文件如下所示:
public class TranscriptTester {
public static void main(String[] args){
Transcript test = new Transcript("11234", "Cody");
test.addCourse("COP2013","A+");
test.addCourse("COP2014","B+");
test.addCourse("COP2015","B+");
test.addCourse("COP2016","D+");
test.addCourse("COP2017","A");
test.addCourse();
test.updateCourse("COP2013", "B-");
test.updateCourse("COP2014", "A+");
test.getGPA();
test.printArray();
test.toString();
}
}
根据我的代码,它应该在 classesTaken 的第一次迭代中跳转到 if 语句,因为 COP2013 的索引为 0。
如果您需要更多信息以帮助我,请询问。谢谢!
如果您想查看所有类文件,这是所有类文件:
public class Course {
//global variables
private String courseID;
private String letterGrade;
private Double numberGrade;
//CONSTRUCTORS
//param
public Course(String id, String letter){
courseID = id;
letterGrade=letter;
compute(letter);
}
//default
public Course(){
courseID = "COP2053";
numberGrade = 4.0;
letterGrade = "A";
compute(getLetterGrade());
}
//COMPUTING GRADES AND UPDATING
private void compute(String letter){
if(letter == "A+"){numberGrade = 4.0;}
else if(letter == "A"){numberGrade = 3.67;}
else if(letter == "A-"){numberGrade = 3.33;}
else if(letter == "B+"){numberGrade = 3.00;}
else if(letter == "B"){numberGrade = 2.67;}
else if(letter == "B-"){numberGrade = 2.33;}
else if(letter == "C+"){numberGrade = 2.00;}
else if(letter == "C"){numberGrade = 1.67;}
else if(letter == "C-"){numberGrade = 1.33;}
else if(letter == "D+"){numberGrade = 1.0;}
else if(letter == "D"){numberGrade = .67;}
else if(letter == "D-"){numberGrade = .33;}
else if(letter == "F"){numberGrade = 0.0;}
}
public void updateGrade(String newGrade){
letterGrade=newGrade;
compute(newGrade);
}
@Override
public String toString(){
return "\nCourse: "+getCourseID()+"\nLetter Grade: "+getLetterGrade()
+"\nGPA in course: "+numberGrade;
}
//GETTERS
public String getCourseID(){
return courseID;
}
public String getLetterGrade(){
return letterGrade;
}
public Double getNumberGrade(){
return numberGrade;
}
}
成绩单文件:
public class Transcript {
private ArrayList<Course> classesTaken = new ArrayList<Course>();
public Transcript(String studentID, String studentName){
System.out.println("\nStudent ID: "+studentID+"\nStudent Name: "+studentName);
}
public void addCourse(String courseID, String letterGrade){
Course myCourse = new Course(courseID,letterGrade);
classesTaken.add(myCourse);
}
public void addCourse(){
Course myCourse = new Course();
classesTaken.add(myCourse);
}
public void updateCourse(String courseID, String newLetterGrade){
for(int i= 0; i<classesTaken.size(); i++){
if(classesTaken.get(i).equals(courseID)){
classesTaken.get(i).updateGrade(newLetterGrade);
}
}
}
public void getGPA(){
System.out.println("\nOverall GPA: "+calculateGPA());
}
private Double calculateGPA(){
Double overallGPA = 0.0;
for(int i=0; i<classesTaken.size(); i++){
overallGPA = classesTaken.get(i).getNumberGrade()+overallGPA;
}
return overallGPA / classesTaken.size();
}
public void printArray(){
for(int i=0; i<classesTaken.size(); i++){
System.out.println("\n"+classesTaken.get(i));
}
}
}