[英]Can someone help me with this code, it's not returning the results I want to
任何人都可以编译这段代码并帮助我,因为它没有返回我想要的结果。 我不知道是返回不起作用还是什么,请帮助我,这是代码:
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,但您没有在任何地方打印它。 您可以使用挥杆中的警报框来显示结果。
在主 function 代替。,
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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.