java - 计算出来0.00

标签 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());
    }
}

最佳答案

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

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

关于java - 计算出来0.00,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23439776/

相关文章:

java - 当使用RabbitMQ作为java工作队列时,应该如何处理并发和 transient 错误?

java - 使用 jar 更新 list 并覆盖重复条目

java - 如何识别文本文件中的字符并将其转换为 map 中的对象?

java - 使用两个不同的标准对数组进行排序

java - 收集的意义是什么<?>

java - SQLiteJDBC 和 PreparedStatement 使用 pragma table_info

java - 如何使用节点在堆数据结构中实现渗透功能?

java - 高负载应用程序的数据库可扩展性?

java - 无法在 AsyncTask 中为 ProgressDialog 调用 Looper.prepare() 的线程内创建处理程序

java - 如何将 HashMap 转换为可缓存的 map