微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有人可以帮助我使用此代码,它没有返回我想要的结果

如何解决有人可以帮助我使用此代码,它没有返回我想要的结果

任何人都可以编译此代码并帮助我,因为它没有返回我想要的结果。我不知道是返回不起作用还是什么,请帮助我,代码如下:

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,但没有在任何地方打印它。您可以使用摆动中的警报框来显示结果。

在 main 函数中而不是 .,

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。