[英]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.