我试着做一个简单的分数计算程序。但问题是,除了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());
}
}发布于 2014-05-03 03:15:33
您没有在任何地方调用您的getScore方法,因此没有看到所计算的值。您需要将代码更改为:
public String toString() {
//
return "Grammar : " + grammar + " pts.\nSpelling : " + spelling
+ " pts.\nLength : " + correctLength + " pts.\nContent : "
+ content + " pts." + "\nTotal score" + getScore();
}https://stackoverflow.com/questions/23439776
复制相似问题