首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算出0.00

计算出0.00
EN

Stack Overflow用户
提问于 2014-05-03 03:08:01
回答 1查看 75关注 0票数 2

我试着做一个简单的分数计算程序。但问题是,除了0.0之外,它不会打印出一个值。我尝试过几种不同的方法,但都行不通。如果有人仔细看过代码,至少给我一个提示,说明我做错了什么,我会很感激。

代码语言:javascript
复制
//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;
    }
}
代码语言:javascript
复制
//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());
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-03 03:15:33

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

代码语言:javascript
复制
public String toString() {
//
return "Grammar : " + grammar + " pts.\nSpelling : " + spelling
        + " pts.\nLength : " + correctLength + " pts.\nContent : "
        + content + " pts." + "\nTotal score" + getScore();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23439776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档