Calculating average of an array list?

后端 未结 11 862
天涯浪人
天涯浪人 2020-11-29 09:04

I\'m trying to use the below code to calculate the average of a set of values that a user enters and display it in a jTextArea but it does not work properly. Sa

11条回答
  •  清酒与你
    2020-11-29 09:23

    Why use a clumsy for loop with an index when you have the enhanced for loop?

    private double calculateAverage(List  marks) {
      Integer sum = 0;
      if(!marks.isEmpty()) {
        for (Integer mark : marks) {
            sum += mark;
        }
        return sum.doubleValue() / marks.size();
      }
      return sum;
    }
    

提交回复
热议问题