gpt4 book ai didi

java - toString 中的平均成绩

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:08:12 26 4
gpt4 key购买 nike

我正在为我的类(class)做可怕的平均成绩问题并且遇到了很多问题,但这一个似乎是主要问题。我无法将我的方法中计算出的平均值提取到 toString。这是一种方法:

public void average() {

total = (((quiz1 + quiz2) / 40 * 25) + (midterm / 100 * 35) + (finalExam / 100 * 40));
DecimalFormat dec = new DecimalFormat("#.0");
dec.format(total);
return;

}

我正试图把它放在这里:

public String toString(){
return ("Grade Report:\n" ....+ "Total Score " + total + "\n" ....);

最佳答案

average 方法不应该设置任何东西——它应该只返回平均值。然后,您可以在 toString 方法中格式化此数字:

public double average() {
return (((quiz1 + quiz2)/40 * 25) +
(midterm/100 * 35) +
(finalExam/100 * 40));
}

public String toString(){
DecimalFormat dec = new DecimalFormat("#.0");
String averageString = dec.format(average();
return ("The average score is " + averageString); // just an example
}

关于java - toString 中的平均成绩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35975158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com