将数组作为用户输入,输出累加和作为新数组
Take array as input from user and output accumulative sum as new array
我无法让这个程序运行。它应该将一个数组作为输入并输出一个新数组,其中包含输入数组的累加和。我为此使用了一个函数(底部)。
例如:
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;
}
}
使用Arrays.toString()
打印数组。
您不能直接调用 System.out.println(accSum(a));
,因为那样会打印数组的内存地址,而不是内容。用 Arrays.toString(accSum(a))
包装调用,它将打印预期的输出。
PS:您的 post 中的代码无法编译:
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;
}
您可以使用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();
}
另请参阅:
我无法让这个程序运行。它应该将一个数组作为输入并输出一个新数组,其中包含输入数组的累加和。我为此使用了一个函数(底部)。
例如:
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;
}
}
使用Arrays.toString()
打印数组。
您不能直接调用 System.out.println(accSum(a));
,因为那样会打印数组的内存地址,而不是内容。用 Arrays.toString(accSum(a))
包装调用,它将打印预期的输出。
PS:您的 post 中的代码无法编译:
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;
}
您可以使用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();
}
另请参阅: