你能帮我发现一下这个bug吗?

时间:2017-04-04 13:47:08

标签: java

所以这是我的代码总共3个类。使用java bluejay

  public class Person
    {
    private String _firstName;
    private String _lastName;
    private long _id;
    public static final String NO_NAME = "";

    public Person(String firstName, String lastName, long id){
    _firstName = firstName;
    _lastName = lastName;
    _id = id;}

    public Person(Person other){
    _firstName = other._firstName;
    _lastName = other._lastName;
    _id = other._id;}

    public String getFirstName(){
    return _firstName;}

    public String getLastName(){
    return _lastName;}

    public long getId(){
    return _id;}

    public void setFirstName(String other){
    this._firstName = other;}

    public void setLastName(String other){
    this._lastName = other;}

    public void setId(long other){
    this._id = other;}

    public String toString(){
    return "Last Name:" + _lastName + "," +"First Name:" + _firstName + "," + "ID:" + _id;}
    }

public class Student extends Person
{
private int _numOfCourses;
private double _averageGrade;
private String[] _courses;
private double[] _grades;
private static final int MAX_NUM_OF_COURSES = 10;

public Student(String firstName, String lastName, long id){
super(firstName,lastName,id);
_courses = new String[MAX_NUM_OF_COURSES];
_grades = new double[MAX_NUM_OF_COURSES];
_numOfCourses = 0;
_averageGrade = 0;
}

public Student(String firstName, String lastName, long id, String[] courses, double[] grades){
this(firstName,lastName,id);
for(int i = 0; i < _numOfCourses; i ++){
    addCourse(courses[i],grades[i]);}}

public int getNumOfCourses(){
return _numOfCourses;}

public double getAverageGrade(){
return _averageGrade;}

public void setNumOfCourses(int other){
this._numOfCourses = other;}

public void setAverageGrade(double other){
this._averageGrade = other;}

public boolean addCourse(String courseName, double courseGrade){
if(_numOfCourses < _courses.length){
   _courses[_numOfCourses] = courseName;
     _grades[_numOfCourses] = courseGrade;
       _numOfCourses++;
          int sumOfGrades = 0; 
              setAverageGrade(averager());
                         return true;}
                            return false;
}

public double averager(){
int sumOfGrades = 0; 
            for(int i = 0; i < _numOfCourses; i++){
                if(_numOfCourses == 0) return _averageGrade = 0;
                   if(_numOfCourses > 0) sumOfGrades += _grades[i];
                      _averageGrade = sumOfGrades / _numOfCourses;
                         }return _averageGrade;
}

public String toString(){
String myString = super.toString();
   for(int i = 0; i < _numOfCourses; i++){
       myString +=  '\n' + '\t' + _courses[i] + ":" + '\t' + _grades[i];}
          return myString;}

和我的主要:

public class Bdikot
{
public static void main(String[]args){
String Maya = "Maya";
String Farber = "Farber"; 
String[] course = new String[5];
double[] grade = new double[5];

course[0] = "math0";
course[1] = "math1";
course[2] = "math2";
course[3] = "math3";
course[4] = "math4";
grade[0] = 50;
grade[1] = 60;
grade[2] = 70;
grade[3] = 80;
grade[4] = 90;
/*
for(int i = 0; i > course.length; i++){
String wierdo = "weirdo";
addCourse(course[i] = wierdo + i,grade[i] = i*20);} 
 */


Student mami = new Student(Maya,Farber,998555999,course,grade);
System.out.println(mami);}    
}

现在的问题是,当我使用main时,它不会打印课程并对数组进行分级,我不知道为什么。 这就是它打印的内容:&#34;姓氏:Farber,名字:Maya,ID:998555999&#34; 你能发现问题吗?

1 个答案:

答案 0 :(得分:0)

  

现在的问题是当我使用main时它不打印课程和   等级阵列,我不知道为什么。这是它的印刷品:“最后   姓名:Farber,名字:Maya,ID:998555999“

问题出在Student构造函数中:

public Student(String firstName, String lastName, long id, String[] courses, double[] grades){
   ...
  for(int i = 0; i < _numOfCourses; i ++){
     addCourse(courses[i],grades[i]);}
  }
   ...
}
作为_numOfCourses实例字段的

int具有0作为默认值。所以,你不要循环。

您应该使用courses数组的长度,并检查coursesgrades数组的长度是否与循环依赖于此条件的长度相同:

public Student(String firstName, String lastName, long id, String[] courses, double[] grades){
   ...
  if (courses.length != grades.length){
      throw new IllegalArgumentException("courses.length != grades.length);
  }
  for(int i = 0; i < courses.length; i ++){
     addCourse(courses[i],grades[i]);}
  }
   ...
}

编辑评论:

您不需要_numOfCourses字段来了解添加的课程数量 使用自动更新的courses数组长度的getter可以检索此信息。

所以替换这个字段:

private int _numOfCourses;

通过这种方法:

private int getNumOfCourses(){
    return courses.length;
}