计算出来0.00

时间:2014-05-03 03:08:01

标签: java

我正在努力制作一个简单的成绩计算程序。但问题是它不会打印除0.0之外的值。我尝试了几种不同的方式,它只是不起作用。如果有人查看代码并给我至少一些我做错的提示,我将不胜感激。

//SUBCLASS OF GRADED ACTIVITY
public class Essay {
    private double grammar;
    private double spelling;
    private double correctLength;
    private double content;
    private double score = 0;

    public void setScore(double gr, double sp, double len, double cnt) {
        grammar = gr;
        spelling = sp;
        correctLength = len;
        content = cnt;
    }

    public void setGrammer(double g) {
        this.grammar = g;
    }

    public void setSpelling(double s) {
        this.spelling = s;
    }

    public void setCorrectLength(double c) {
        this.correctLength = c;
    }

    public void setContent(double c) {
        this.content = c;
    }

    public double getGrammar() {
        return grammar;
    }

    public double getSpelling() {
        return spelling;

    }

    public double getCorrectLength() {
        return correctLength;

    }

    // CALCULATE THE SCORE
    public double getScore() {
        return score = grammar + spelling + correctLength + content;
    }

    public String toString() {
        //
        return "Grammar : " + grammar + " pts.\nSpelling : " + spelling + " pts.\nLength : " + correctLength +
                       " pts.\nContent : " + content + " pts." + "\nTotal score" + score;
    }
}


//Main demo
public class Main {
    public static void main(String[] args) {
        Essay essay = new Essay();
        essay.setGrammer(25);
        essay.setSpelling(15);
        essay.setCorrectLength(20);
        essay.setContent(28);
        System.out.println(essay.toString());
    }
}

1 个答案:

答案 0 :(得分:2)

您无法在任何地方调用getScore方法,因此无法看到计算出的值。您需要将代码更改为:

public String toString() {
//
return "Grammar : " + grammar + " pts.\nSpelling : " + spelling
        + " pts.\nLength : " + correctLength + " pts.\nContent : "
        + content + " pts." + "\nTotal score" + getScore();
}