简体   繁体   English

如何从扫描仪获取多个整数输入并将每个整数存储在单独的数组中?

[英]How can I take multiple integer input from scanner and store each integer in separate arrays?

I'm trying to create a program which allows the user to addition as many pairs of numbers as they would like, by first taking user input to ask them how many sums they would like to complete (additions of 2 numbers), thereafter creating 2 arrays of whatever size the user has input, and then asking the user to input each pair of numbers on a single line that they would like to addition and storing the first value in one array and the second value in a second array from each input. 我正在尝试创建一个程序,该程序允许用户添加任意​​数量的数字对,方法是先接受用户输入,询问他们要完成多少个和(两个数字相加),然后创建2用户输入的任意大小的数组,然后要求用户在一行上输入每对数字,他们希望将每个输入的第一个值加到一个数组中并将第二个值存储在第二个数组中。 This is where I am stuck, I don't know how I can take the user input as two int values on each line and store them in the respective indexes of each array to be added later. 这就是我遇到的问题,我不知道如何将用户输入作为每行上的两个int值并将它们存储在每个数组的相应索引中,以便稍后添加。 Please take a look at my code below: 请在下面查看我的代码:

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

I think you need to change 2 lines this way: 我认为您需要以这种方式更改2行:

a[i] = intinput[0];
b[i] = intinput[1];

Just read pairs with nextInt() method of scanner, it's use all space symbols like separator (not only end of line): 只需使用扫描仪的nextInt()方法读取对,它就会使用所有空格符号(例如分隔符)(不仅是行尾):

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

        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++) {
            a[i] = sc.nextInt();
            b[i] = sc.nextInt();
        }
    }
}

You're using nextLine() method, which is useful for string input, but it's not the best solution for integer or other primitive data. 您正在使用nextLine()方法,该方法对于字符串输入很有用,但它不是整数或其他原始数据的最佳解决方案。

In addition, this line of code is wrong : 另外,这行代码是错误的:

a = intinput[0];

because you're storing an integer value as integer array. 因为您要将整数值存储为整数数组。 You must store that value inside a[i] in order to respect the variables type. 您必须将该值存储在a [i]中,以便遵守变量类型。

I'd do it this way: 我会这样:

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

        for (int i = 0; i < a.length; i++) {
            System.out.println("Please enter the values you would like to sum as pairs of two numbers: ");
            // Read pair and store it inside i-th position of a and b arrays 
            System.out.println("Enter first number: ");
            a[i] = sc.nextInt();
            System.out.println("Enter second number: ");
            b[i] = sc.nextInt();
        }
        // Close scanner
        sc.close();

        // Prints every sum
        for(int i = 0; i < a.length; i++){
            System.out.println(i + "-th sum is: " + (a[i] + b[i]));
        }
    }
}

Here you read every pair with nextInt() which is specific for integer data. 在这里,您可以使用针对整数数据的nextInt()读取每一对。

Every time you store items in i-th position of arrays, so finally you can sum a[i] and b[i] . 每次将项目存储在数组的第i个位置时,最后可以将a[i]b[i]相加。

Result example: 结果示例:

Enter the amount of sums you would like to calculate: 
4
Please enter the values you would like to sum as pairs of two numbers: 
Enter first number: 
2
Enter second number: 
1
Please enter the values you would like to sum as pairs of two numbers: 
Enter first number: 
3
Enter second number: 
4
Please enter the values you would like to sum as pairs of two numbers: 
Enter first number: 
5
Enter second number: 
6
Please enter the values you would like to sum as pairs of two numbers: 
Enter first number: 
7
Enter second number: 
8
0-th sum is: 3
1-th sum is: 7
2-th sum is: 11
3-th sum is: 15

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM