简体   繁体   English

如何在 java 中单行取多个输入

[英]How to take multiple inputs in java in a single line

在此处输入图像描述

   BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    int num = Integer.parseInt(br.readLine()); //Length of Array
    int t = num * 2;
    String s;
    for(int i = 0; i<num; i++) {    
        s = br.readLine();
    }
    int[] arr= new int[t];
    String[] s1 = s.split(" ");
    for(int i=0;i<num;i++)
    {
        arr[i]=Integer.parseInt(s1[i]);
    }
    for(int j = 0; j< arr.length; j++) {
        System.out.println(arr[j]);
    }

Here i have attempted to taken a value in variable num and i want to take input the number of times in a single line according to variable num but if i am printing the arr i am getting only two values which i have given the input in last rest are 0. I think s is replaced with the new input i have entered please help me to solve this在这里,我试图在变量 num 中取一个值,并且我想根据变量 num 在一行中输入次数,但是如果我正在打印 arr,我只会得到两个值,我在最后给出了输入rest 为 0。我认为 s 已替换为我输入的新输入,请帮我解决这个问题

You can add string like this:您可以像这样添加字符串:

  public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int num = Integer.parseInt(br.readLine()); // Length of Array
        String s = "";
        for (int i = 0; i < num; i++) {
            s += br.readLine() + " ";
        }
        int[] arr = new int[num * 2];
        String[] s1 = s.split("[\\s]");
        for (int i = 0; i < s1.length; i++) {
            arr[i] = Integer.parseInt(s1[i]);
        }

        System.out.println(Arrays.toString(arr));
    }

, output , output

2
1 2
3 4
[1, 2, 3, 4]

You can try this:你可以试试这个:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num = 0; //Length of Array
try {
    num = Integer.parseInt(br.readLine());
} catch (IOException e) {
    e.printStackTrace();
}
int t = num * 2;
int[] arr= new int[t];
String s;
for(int i = 0, j = 0; i<num; i++) {
    try {
        s = br.readLine();
        String[] s1 = s.split(" ");
        arr[j++] = Integer.parseInt(s1[0]);
        arr[j++] = Integer.parseInt(s1[1]);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

for(int j = 0; j< arr.length; j++) {
    System.out.println(arr[j]);
}

You can use Scanner Class to directly take the input as integer.您可以使用 Scanner Class 直接将输入作为 integer。

import java.util.Scanner;

public class prac {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(System.in);
    int num, i;
    num = in.nextInt();
    int ar[][] = new int[num][2];
    for (i = 0; i < num; i++) {
      ar[i][0] = in.nextInt();
      ar[i][1] = in.nextInt();
    }

    for (i = 0; i < num; i++) {
      System.out.println(ar[i][0] + " " + ar[i][1]);
    }
  }

}

OUTPUT: OUTPUT:

  2
  1 5
  6 8
  1 5 //This is the Output
  6 8

The above code is storing input in 2D array.上面的代码将输入存储在二维数组中。 You could store the same in 1D array also.您也可以将其存储在一维数组中。

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

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