有人能编译这段代码并帮助我吗?因为它没有返回我想要的结果。我不知道退货是否不起作用,请帮帮我,这是代码:
public class ComputeAverage {
public static void main (String[] args) {
new ComputeAverage().computeAverage(4);
}
public double computeAverage(int how_many)
{ double total_points = 0.0; // the total of all test scores
int count = 0; // the quantity of tests read so far
while ( count != how_many )
// at each iteration: total_points == exam_1 + exam_2 + ... + exam_count
{ // ask the user for the next exam score:
String input = JOptionPane.showInputDialog("Type next exam score:");
int score = new Integer(input).intValue();
if (score < 0) {
JOptionPane.showMessageDialog(null, "Enter a positive numer");
total_points = total_points - score;
}
// add it to the total:
total_points = total_points + score;
count = count + 1;
// print a summary of the progress:
System.out.println("count = " + count + "; total = " + total_points);
}
// at conclusion: total_points == exam_1 + exam_2 + ... + exam_how_many
return (total_points / how_many);
}
}
您正在将值返回到main,但没有将其打印到任何位置。您可以在swing中使用警告框来显示结果。
在主函数中,而不是。,
public static void main (String[] args) {
new ComputeAverage().computeAverage(4);
}
尝试:
public static void main (String[] args) {
JOptionPane.showMessageDialog(null,"Exam score : " + new ComputeAverage().computeAverage(4));
}