如何仅从数组列表中计算平均值“计算正数而忽略负数)?

我正在尝试从数组列表中计算正平均值。它从数组列表中获取值,但仅计算“正”数字,同时进行测试


Integer[] array = new Integer[]{3, 2, -4, -7}; 

ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(array));

System.out.printf(averagePositive(arrayList));

我尝试过使用腹肌,但它不起作用。


public static double averagePositive(ArrayList<Integer> values) {

    if (values == null || values.isEmpty())

       return 0.0;


    int sum = 0;

    int n = values.size();


    for (int i = 0; i < n  ; i++)

       if (values.get(i) > 0.0) {

           sum += values.get(i);

    }


    return ((double) sum) / n;

}

我想要的输出是2.50,但得到1.25


交互式爱情
浏览 71回答 5
5回答

慕慕森

您应该计算正数并除以该计数,而不是除以:nint count = 0;for (int i = 0; i < n&nbsp; ; i++) {&nbsp; &nbsp; if (values.get(i) > 0.0) {&nbsp; &nbsp; &nbsp; &nbsp; sum += values.get(i);&nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; }}if (count > 0) {&nbsp; &nbsp; return ((double) sum) / count;} else {&nbsp; &nbsp; return 0;}

倚天杖

Your 等于数组的大小。相反,它应该是总正数的计数npublic static void main(String[] args) {&nbsp; &nbsp; Integer[] array = new Integer[] { 3, 2, -4, -7 };&nbsp; &nbsp; ArrayList arrayList = new ArrayList(Arrays.asList(array));&nbsp; &nbsp; System.out.println(averagePositive(arrayList));}public static double averagePositive(ArrayList<Integer> values) {&nbsp; &nbsp; if (values == null || values.isEmpty())&nbsp; &nbsp; &nbsp; &nbsp; return 0.0;&nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; int n = 0;&nbsp; &nbsp; for (int i = 0; i < values.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; if (values.get(i) > 0.0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += values.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; return ((double) sum) / n;}

梵蒂冈之花

您需要将总和除以正元素的数量,而不是总大小。public static double averagePositive(ArrayList<Integer> values) {if (values == null || values.isEmpty())&nbsp; &nbsp;return 0.0;int sum = 0;int positive_n = 0;for (int i = 0; i < n&nbsp; ; i++)&nbsp; &nbsp;if (values.get(i) > 0.0) {&nbsp; &nbsp; &nbsp; &nbsp;sum += values.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;positive_n++;}return ((double) sum) / positive_n;}

繁华开满天机

您的代码几乎是正确的。你唯一缺少的部分是你想取正元素的平均值,所以在你的情况下,元素的总量应该是2。您应该只计算正元素,如下所示:int n = 0;for (int i = 0; i < n&nbsp; ; i++)&nbsp; &nbsp;if (values.get(i) > 0.0) {&nbsp; &nbsp; &nbsp; &nbsp;sum += values.get(i);&nbsp; &nbsp; &nbsp; &nbsp;n++;}这应该可以解决问题。

天涯尽头无女友

您应该计算有多少个正值,而不是除以列表大小:int positiveCount = 0;for (int i = 0; i < n&nbsp; ; i++)&nbsp; &nbsp; &nbsp; &nbsp;if (values.get(i) > 0.0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sum += values.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;positiveCount++;}return ((double) sum) / positiveCount;或者,您可以使用流:public static double averagePositive(List<Integer> values) {&nbsp; &nbsp; return values.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.filter(d -> d > 0.0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.mapToDouble(Function.identity())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.average().orElse(0.0);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java