简体   繁体   English

如何使用扫描仪在hackerrank中的Java中以空格分隔的字符输入

[英]how to take input of characters separated by space in java in hackerrank using scanner

hi i want to know how to take input of characters separated Space" " in java using scanner for example first line contains (int)T (no of test cases) second line contains {int(N)}size of an character array separated space then contains {int (X)} for some operation then in next line it contains (char ch(N) with input 1 and 0 and each character is separated by space 嗨,我想知道如何使用扫描仪在Java中输入字符分隔的空格“”第一行包含(int)T(无测试用例)第二行包含{int(N)}个字符数组分隔的空格的大小然后包含{int(X)}用于某些操作,然后在下一行包含(char ch(N),其输入为1和0,并且每个字符都由空格分隔

sample: 样品:

图片1

You can use scanner.nextInt() method , it will take care of new line and space character. 您可以使用scanner.nextInt() 方法 ,它将处理换行符和空格字符。 Please find below code snippet. 请在下面找到代码片段。

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
for (int i=0;i<x;i++) {
    int temp = sc.nextInt();
    System.out.println(temp);
}

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

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