简体   繁体   English

将数组作为用户输入,output 累计和作为新数组

[英]Take array as input from user and output accumulative sum as new array

I can't get this program to work.我无法让这个程序工作。 It's supposed to take an array as input and output a new array with the accumulative sum of the input array.它应该将一个数组作为输入,output 一个新数组,输入数组的累积和。 I used a function for this (bottom part).我为此使用了 function(底部)。

For example:例如:

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

Here's my program:这是我的程序:

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

Use Arrays.toString() to print an array.使用Arrays.toString()打印一个数组。

You can't call System.out.println(accSum(a));你不能调用System.out.println(accSum(a)); directly as that will print the memory address of the array and not the contents.直接因为这将打印数组的 memory 地址而不是内容。 Wrap the call with Arrays.toString(accSum(a)) and it will print the expected output.Arrays.toString(accSum(a))包装调用,它将打印预期的 output。

PS: The code in your post doesn't compile: PS:您帖子中的代码无法编译:

  • scanner.close(); should be s.close();应该是s.close();
  • accSum() should be static. accSum()应该是 static。

Addendum: So your full code became this:附录:所以你的完整代码变成了这样:

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

You can use Arrays.stream(int[],int,int) method to get the sum of the previous array elements:您可以使用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();
}

See also: Modifying a 2D array另请参阅:修改二维数组

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 java 在数组中获取用户输入? - how to take user input in Array using java? 如何在 java 中输入数组列表和 output 和 - How to input an array list and output sum in java 用户输入后剩余数组编号的总和 - sum of remaining array numbers after user input 二维数组从用户获取浮点输入并计算列的总和 - 2D Array to take float inputs from user and calculate sum of columns 使用数组而不是使用Java中的ArrayList从用户输入创建新对象 - Create a new object from user input using array and not ArrayList in Java 如何从文件中获取数组输入,对其进行排序并在同一文件中显示 output - How to take array input from a file ,sort it and display output in the same file 如何将循环输出作为新的数组输入 - How to put for loop output as a new array input 从用户那里获取 6 位数字输入并将每个数字存储在 Java 中的单个数组中 - Take 6 digit input from user and store each digit in a single array in Java 我想使用增强型for循环,从用户处获取输入并在数组中找到元素,为什么该程序未相应运行? - I want to use enhanced for loop, take input from user and find the element in the array, Why is this program not running accordingly? 从用户输入 1-10 次 5 次并将它们存储在一个数组中,如果输入错误,则提示输入正确 - Take input from user between 1-10 five times and store them in an array, if wrong input given then prompt for correct input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM