0
我有以下代码扫描仪无法扫描第一字符串
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int noOfTestCases = scanner.nextInt();
String[] firstStringsList = new String[noOfTestCases];
String[] secondStringsList = new String[noOfTestCases];
for(int i=0;i<noOfTestCases;i++){
firstStringsList[i] = scanner.nextLine();
secondStringsList[i] = scanner.nextLine();
}
for(int i=0;i<noOfTestCases;i++){
System.out.println("First String : " + firstStringsList[i]);
System.out.println("Second String : " + secondStringsList[i]);
}
}
和输出变为
1
asd
First String :
Second String : asd
即我首先输入1,这意味着我有其涉及获得two String inputs(per test case).
1测试用例但是我只能输入1个分配给secondStringsList[i]
的字符串。我正在使用Intellij IDEA
。这是为什么发生?解决方法?
你搜索涉及的问题'nextLine()'上'Scanner'回事?这个确切的问题经常被问到。 –
[在nextLine()之前使用nextInt()](http://christprogramming.wordpress.com/2014/01/30/java-common-mistakes-1/) – Christian
以下是我之前准备的两个! :-) http://stackoverflow.com/questions/20751385/setting-a-scanner-as-a-global-variable http://stackoverflow.com/questions/20181887/java-string-variable-using-next- or-nextline –