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

时间:2017-06-02 22:49:26

标签: java arrays loops

我正在尝试创建一个程序,允许用户添加尽可能多的数字对,首先通过用户输入询问他们想要完成多少总和(添加2个数字),然后创建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];
}
}

3 个答案:

答案 0 :(得分:1)

我认为您需要以这种方式更改2行:

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

答案 1 :(得分:0)

只需使用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();
        }
    }
}

答案 2 :(得分:0)

你正在使用nextLine()方法,这对字符串输入很有用,但它不是整数或其他原始数据的最佳解决方案。

此外,这行代码错误:

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

因为您将整数值存储为整数数组。 您必须将该值存储在[i]中,以便尊重变量类型。

我这样做:

a[i]

在这里,您将使用nextInt()读取每一对,这对于整数数据。

每次将项目存储在数组的第i个位置时,最后您可以将b[i]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 相加。

结果示例:

class spawn(object):
def __init__(self,place1,place2):
    self.place1=place1
    self.place2=place2
def AIPrototype(self):#The important parts to this error star here
    global x,y,x1,y1
    pygame.draw.rect(screen,THECOLORS['blue'],(self.place1,self.place2,50,50))
    x1=self.place1
    y1=self.place2#end here
    if x1<x:
        xspeed1=1
        slopex1=x-x1
    if x1>x:
        xspeed1=-1
        slopex1=x1-x
    if y1<y:
        yspeed1=1
        slopey1=y-y1
    if y1>y:
        yspeed1=-1
        slopey1=y1-y       
#
    hypo1=((slopex1**2)+(slopey1**2))**0.5
    speedmark1=hypo1/3
    speedy1=slopey1/speedmark1
    speedx1=slopex1/speedmark1
    movex1=speedx1
    movey1=speedy1
    if x1<=640 and x1>=0:
        if x1>x:
            x1+=xspeed1*movex1
            if x1<x:
                xspeed1=0
    if y1<=480 and x1>=0:
        if y1>y:
            y1+=yspeed1*movey1
            if y1<y:
                yspeed1=0
    if x1<=640 and x1>=0:
        if x1<x:
            x1+=xspeed1*movex1
            if x1>x:
                xspeed1=0
    if y1<=480 and x1>=0:
        if y1<y:
            y1+=yspeed1*movey1
            if y1>y:
                yspeed1=0
#
    if x1>640:
        x1=640
    if x1<0:
        x1=0
    if y1>480:
        y1=480
    if y1<0:
        y1=0 
    self.place1=x1#start
    self.place2=y1#end