没有返回正确的值

时间:2014-10-01 12:51:25

标签: java

主类:

public class Student
{
    private int numberOfQuizzes;
    private double newQuizScore;
    private double averageScore;
    private double totalScore;
    private double average;
    private double gpa;
    private double finalGpa;
    private double gpa1;
    private double gpa2;
    private String className;
    private double gpa3;
    private boolean addQuiz;
    private String studentName;
    public Student(String studentName)
    {
        this.studentName = studentName;
    }
    public void addQuiz(double newQuizScore)
    {
        this.newQuizScore = newQuizScore;
        if(newQuizScore >= 0)
        {
            totalScore =  totalScore + newQuizScore;
            numberOfQuizzes++;
        }
    }
    public void setAverageScore()
    {
        averageScore = totalScore/numberOfQuizzes;
    }
    public void resetScore()
    {
        averageScore = 0;
        totalScore = 0;
        numberOfQuizzes = 0;
        newQuizScore = 0;
    }
    public void setGpaValue(double average, String className)
    {
    if(className.equalsIgnoreCase("Biology"))
    {
        if( average >= 93.0)
        {
            gpa1 = 4.0;
        } else if(average < 93.0 && average >= 90.0)
        {
            gpa1 = 3.66;
        } else if(average < 90.0 && average >= 87.0)
        {
            gpa1 = 3.33;
        } else if(average < 87.0 && average >= 83)
        {
            gpa1 = 3.00;
        } else if(average < 84.0 && average >= 80.0)
        {
            gpa1 = 2.66;
        } else if(average < 80 && average >= 77)
        {
            gpa1 = 2.33;
        } else if(average < 77 && average >= 73)
        {
            gpa1 = 2.00;
        } else if(average < 73 && average >= 70)
        {
            gpa1 = 1.66;
        } else if(average < 70 && average >= 67)
        {
            gpa1 = 1.33;
        } else if(average < 67 && average >= 63)
        {
            gpa1 = 1.00;
        } else if(average < 63 && average >= 60.0)
        {
            gpa1 = .66;
        } else if(average < 60 && average >= 0)
        {
            gpa1 = .00;
        } 
    }
    if(className.equalsIgnoreCase("Calculus"))
    {
        if( average >= 93.0)
        {
            gpa2 = 4.0;
        } else if(average < 93.0 && average >= 90.0)
        {
            gpa2 = 3.66;
        } else if(average < 90.0 && average >= 87.0)
        {
            gpa2 = 3.33;
        } else if(average < 87.0 && average >= 83)
        {
            gpa2 = 3.00;
        } else if(average < 84.0 && average >= 80.0)
        {
            gpa2 = 2.66;
        } else if(average < 80 && average >= 77)
        {
            gpa2 = 2.33;
        } else if(average < 77 && average >= 73)
        {
            gpa2 = 2.00;
        } else if(average < 73 && average >= 70)
        {
            gpa2 = 1.66;
        } else if(average < 70 && average >= 67)
        {
            gpa2 = 1.33;
        } else if(average < 67 && average >= 63)
        {
            gpa2 = 1.00;
        } else if(average < 63 && average >= 60.0)
        {
            gpa2 = .66;
        } else if(average < 60 && average >= 0)
        {
            gpa2 = .00;
        } 
    }
    if(className.equalsIgnoreCase("CompSci"))
    {
        if( average >= 93.0)
        {
            gpa3 = 4.0;
        } else if(average < 93.0 && average >= 90.0)
        {
            gpa3 = 3.66;
        } else if(average < 90.0 && average >= 87.0)
        {
            gpa3 = 3.33;
        } else if(average < 87.0 && average >= 83)
        {
            gpa3 = 3.00;
        } else if(average < 84.0 && average >= 80.0)
        {
            gpa3 = 2.66;
        } else if(average < 80 && average >= 77)
        {
            gpa3 = 2.33;
        } else if(average < 77 && average >= 73)
        {
            gpa3 = 2.00;
        } else if(average < 73 && average >= 70)
        {
            gpa3 = 1.66;
        } else if(average < 70 && average >= 67)
        {
            gpa3 = 1.33;
        } else if(average < 67 && average >= 63)
        {
            gpa3 = 1.00;
        } else if(average < 63 && average >= 60.0)
        {
            gpa3 = .66;
        } else if(average < 60 && average >= 0)
        {
            gpa3 = .00;
        } 
    }
    }
    public void setTotalGpa()
    {
        finalGpa = (gpa1 * gpa2 * gpa3)/3;
    }
    public double getAverageScore()
    {
        return averageScore;
    }
    public double getTotalGpa()
    {
        return finalGpa;
    }
    public double totalScore()
    {
        return totalScore;
    }
    public double getGpaValue()
    {
        return average;
    }
    public String toString()
    {
        String toString = "\nStudent Name: " + studentName +
                          "\nStudent Gpa: " + finalGpa;
        return toString;
    }
}

测试人员类: http://pastebin.com/meh3Vqvq

我遇到问题,gpa总是返回0.0。除了gpa不起作用外,其他所有功能似乎都能正常运行。

1 个答案:

答案 0 :(得分:0)

private double average;
...
public double getGpaValue()
{
    return average;
}

此方法返回从未分配的成员,因此默认情况下其值仍为0.0。

您正在将average传递给public void setGpaValue(double average, String className)方法,但不将其存储在实例变量中。