我的计划试图找出学生考试成绩的范围,平均值和高分

时间:2014-10-21 12:56:07

标签: java

getRange()有效,getAverage()有效,但getHighScore()似乎不起作用。总是得到答案0.我在我的代码中使用4点系统获得高分,所以如果一个人得到平均值95和5的范围,他们将得到5的高分。然后分开乘以5并乘以4以保持4的答案。

public int getAverage() //Gets average of test scores
{
    //Compute and return the average
    int average;
    average = (int) Math.round((test1 + test2 + test3) / 3.0); //Rounds the average number
    return average;
}

public int getRange() //Gets range of test scores
    {
        int range;
        range = 0;
        if (test1 >= test2 && test1 >= test3)
            if (test2 >= test3)
                range = test1-test3;
            else if (test3 >= test2)
                range = test1-test2;
        if (test2 >= test1 && test2 >= test3)
            if (test1 >= test3)
                range = test2-test3;
            else if (test3 >= test1)
                range = test2-test1;
        if (test3 >= test1 && test3 >= test2)
            if (test1 >= test2)
                range = test3-test2;
            else if (test2 >= test1)
                range = test3-test1;
        return range;
    }

public double getHighScore() //Formula for finding highscore with the use of getAverage() and getRange()

{
        double highscore;
        highscore = 0;
        if (getAverage() >= 90)
            highscore = highscore + 4;
        else if (getAverage() >= 80 && getAverage() < 90)
            highscore = highscore + 3;
        else if (getAverage() >= 70 && getAverage() < 80)
            highscore = highscore + 2;
        else if (getAverage() >= 60 && getAverage() < 70)
            highscore = highscore + 1;
        else
            highscore = highscore + 0;
        if (getRange() <= 10)
            highscore = highscore + 1;
        else if (getRange() <=20 && getRange() > 10)
            highscore = highscore + (3/4);
        else if (getRange() <=30 && getRange() > 20)
            highscore = highscore + (2/4);
        else if (getRange() <=40 && getRange() > 30)
            highscore = highscore + (1/4);
        else
            highscore = highscore+0;
        highscore = highscore*(4/5);
        return highscore;
    }

3 个答案:

答案 0 :(得分:3)

问题可能在于这一行:

highscore = highscore*(4/5);

原因是,整数除法(4/5)返回0,你可以尝试:

highscore = highscore*(4.0/5.0);

将所有整数除法更改为十进制除法。

答案 1 :(得分:2)

您的问题在于返回highscore

之前的行
highscore = highscore * (4 / 5);

具体来说,(4/5)总是会产生0,因为它是整数分区。

事实上,你可以在多个地方使用它们,例如:

highscore = highscore + (3/4);
highscore = highscore + (2/4);
highscore = highscore + (1/4); 

答案 2 :(得分:0)

我确定您在学习之初只是一名学生,但此代码无法扩展。

考虑将事物分解为可以单独测试的较小方法。

public class Stats {

    public static double sum(double [] values) {
        double sum = 0.0;
        if (values != null) {
            for (double value : values) {
                sum += value;
            }
        }
        return sum;
    }

    public static double min(double [] values) {
        double minValue = Double.MAX_VALUE;
        if (values != null) {
            for (double value : values) {
                if (value < minValue) {
                    minValue = value;
                }
            }
        }
        return minValue;
    }


    public static double max(double [] values) {
        double maxValue = Double.MIN_VALUE;
        if (values != null) {
            for (double value : values) {
                if (value > mAXValue) {
                    maxValue = value;
                }
            }
        }
        return maxValue;
    }

    public static double ave(double [] values) {
        double ave = 0.0;
        if (values != null && values.length > 0) {
            ave = sum(values)/values.length;
        }
        return ave;
    }
}