简体   繁体   English

有人可以帮我处理这段代码吗,它没有返回我想要的结果

[英]Can someone help me with this code, it's not returning the results I want to

Can anyone compile this code and help me because it's not returning the results I want.任何人都可以编译这段代码并帮助我,因为它没有返回我想要的结果。 I don't know if the return is not working or what, please help me, here is the code:我不知道是返回不起作用还是什么,请帮助我,这是代码:

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);
 
}
 

 }

You are returning the value back to the main but you are not printing it anywhere.您正在将值返回给 main,但您没有在任何地方打印它。 you can use the alert box in swings to show the result.您可以使用挥杆中的警报框来显示结果。

In main function instead of.,在主 function 代替。,

public static void main (String[] args) {
   new ComputeAverage().computeAverage(4);
}

try:尝试:

public static void main (String[] args) {
    JOptionPane.showMessageDialog(null,"Exam score : " + new ComputeAverage().computeAverage(4));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM