【问题标题】:Take array as input from user and output accumulative sum as new array将数组作为用户的输入,将累加和作为新数组输出
【发布时间】:2021-02-26 11:24:38
【问题描述】:

我无法让这个程序运行。它应该将一个数组作为输入并输出一个新数组,其中输入数组的累积和。我为此使用了一个函数(底部)。

例如:

Input: 1 2 3 4
Output: 1 3 6 10

这是我的程序:

import java.util.Scanner;

public class Accumulate {
    public static void main(String[] args) {
        int n, sum = 0;
        Scanner s = new Scanner(System.in);

        System.out.print("Enter the size of the array:");
        n = s.nextInt();
        int a[] = new int[n];

        System.out.println("Enter all the elements:");
        for (int i = 0; i < n; i++) {
            a[i] = s.nextInt();
        }
        System.out.println(Arrays.toString(accSum(a)));
        s.close();
    }

    public static int[] accSum(int[] in) {
        int[] out = new int[in.length];
        int total = 0;
        for (int i = 0; i < in.length; i++) {
            total += in[i];
            out[i] = total;
        }
        return out;
    }
}

【问题讨论】:

  • 请说明该程序在何种情况下无法运行。你得到一个编译错误。如果没有,你会得到一个例外。尝试在 accSum 函数的入口处设置断点,以检查数组是否包含输入的数据。
  • 这能回答你的问题吗? What's the simplest way to print a Java array?
  • 示例:(数组大小)输入:5; (输入所有元素)输入:1 2 3 4 5。我得到一个 ArrayIndexOutOfBounds 异常,“Index 5 out of bounds for length 5”。
  • 检查你的大括号如何匹配。特别是,查看 accSum 定义之前的大括号。确保你的缩进是正确的,否则你会感到困惑。此外,使用 4 个空格进行缩进是常规的(但不是必需的)。

标签: java arrays sum accumulate


【解决方案1】:

使用Arrays.toString() 打印一个数组。

您不能直接调用System.out.println(accSum(a));,因为这将打印数组的内存地址而不是内容。用Arrays.toString(accSum(a)) 包装调用,它将打印预期的输出。

PS:您帖子中的代码无法编译:

  • scanner.close(); 应该是 s.close();
  • accSum() 应该是静态的。

附录: 所以你的完整代码变成了这样:

public static void main(String[] args) {
    int n, sum = 0;

    Scanner s = new Scanner(System.in);

    System.out.print("Enter the size of the array:");
    n = s.nextInt();
    int a[] = new int[n];

    System.out.println("Enter all the elements:");
    for (int i = 0; i < n; i++) {
        a[i] = s.nextInt();
    }
    System.out.println(Arrays.toString(accSum(a)));

    s.close();
}
public static int[] accSum(int[] in) {
    int[] out = new int[in.length];
    int total = 0;
    for (int i = 0; i < in.length; i++) {
        total += in[i];
        out[i] = total;
    }
    return out;
}

【讨论】:

  • 我在问题中编辑了我的代码,当我编译它时,我的函数的第一行 int[] 出现错误,知道吗?
  • 我已经编辑了答案以包含使它在我的机器上工作的完整代码。请看一看。
  • 哦,等等,我看到您编辑了问题中的代码。 :D (就像你说的)你做了Arrays.toString(Sytem.out.print(accSum(a))); 但它应该是System.out.println(Arrays.toString(accSum(a)));
  • 嘿,我更改了问题中的代码,现在当我编译时,我在System.out.println(Arrays.toString(accSum(a))); 得到一个错误,它说"error: cannot find symbol",在“Arrays.”的“A”处有一个标记
  • 嗯......你确实需要正确的导入:import java.util.Arrays;
【解决方案2】:

可以使用Arrays.stream(int[],int,int)方法获取前面数组元素的总和:

public static void main(String[] args) {
    int[] arr1 = {1, 2, 3, 4};
    int[] arr2 = accSum(arr1);
    System.out.println(Arrays.toString(arr2));
    // [1, 3, 6, 10]
}
public static int[] accSum(int[] arr) {
    return IntStream
            // iterate over the indices of the array
            .range(0, arr.length)
            // sum of the current element and all previous elements
            .map(i -> arr[i] + Arrays.stream(arr, 0, i).sum())
            // return an array
            .toArray();
}

另见:Modifying a 2D array

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-26
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 2017-12-21
    相关资源
    最近更新 更多