【发布时间】:2017-06-02 22:49:26
【问题描述】:
我正在尝试创建一个程序,该程序允许用户添加任意数量的数字对,方法是首先接受用户输入询问他们想要完成多少总和(2 个数字的加法),此后创建两个用户输入的任何大小的数组,然后要求用户在一行上输入他们想要添加的每对数字,并将第一个值存储在一个数组中,将第二个值存储在第二个数组中每个输入。这就是我卡住的地方,我不知道如何将用户输入作为每行上的两个 int 值并将它们存储在每个数组的相应索引中,以便稍后添加。请看下面我的代码:
import java.util.Scanner;
public class SumsInLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the amount of sums you would like to calculate: ");
int n = sc.nextInt();
int a[] = new int[n];
int b[] = new int[n];
String[] input = new String[2];
System.out.println("Please enter the values you would like to sum as pairs of two numbers: ");
for (int i = 0; i < a.length; i++) {
input = sc.nextLine().split(" ");
int[] intinput = Arrays.asList(input).stream().mapToInt(Integer::parseInt).toArray();
a = intinput[0];
b = intinput[1];
}
}
【问题讨论】: