【问题标题】:creating a method that sums up the values in the array and returns the total [duplicate]创建一个对数组中的值求和并返回总数的方法[重复]
【发布时间】:2020-11-20 02:03:36
【问题描述】:

如何将数组中的所有值相加并从方法返回总数

double[] dvals = { 88.94, 77.56, -7.0, -203.09 }; // initialize array
double[] dvals2 = new double[8];
System.out.printf("Output of sumAll for dvals : %9.3f\n", sumAll(dvals));
System.out.printf("Output of sumAll for dvals2: %9.3f\n\n", sumAll(dvals2));

【问题讨论】:

标签: java arrays methods sum


【解决方案1】:

有很多好的网站和 StackOverflow 问题可以回答这个问题,但我还是会回答。您可以使用 for 循环并在每次迭代时添加到变量 sum,或者您可以简单地使用 Arrays.stream(arr).sum()

【讨论】:

    猜你喜欢
    • 2020-11-02
    • 2016-04-05
    • 1970-01-01
    • 2022-01-15
    • 2020-07-27
    • 2016-02-19
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多