java键盘录入数存到两组数组,通过输入数组一,输出数组二
时间: 2024-05-14 14:13:58 浏览: 66
java从键盘输入数组
以下是Java代码示例:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 输入数组一
System.out.print("请输入数组一的长度:");
int n = scanner.nextInt();
int[] arr1 = new int[n];
System.out.println("请输入" + n + "个整数:");
for (int i = 0; i < n; i++) {
arr1[i] = scanner.nextInt();
}
// 处理数组一,得到数组二
int[] arr2 = new int[n];
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) {
if (j != i) {
sum += arr1[j];
}
}
arr2[i] = sum;
}
// 输出数组二
System.out.println("数组二的内容为:");
for (int i = 0; i < n; i++) {
System.out.print(arr2[i] + " ");
}
}
}
```
运行示例:
```
请输入数组一的长度:5
请输入5个整数:
1 2 3 4 5
数组二的内容为:
14 13 12 11 10
```
阅读全文