我正在研究java I/O。我的代码有一些问题。 我要打印
[0, 0, 1]
[1, 0, 1]
[0, 0, 1]
[2, 0, 1]
[10, 0, 5]
但需要额外输入。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
sc.nextLine();
System.out.println();
for (int i = 0; i < tc; i++) {
int line = sc.nextInt();
sc.nextLine();
for (int j = 0; j < line; j++) {
System.out.println(i+""+j);
int[] st = Arrays.stream(sc.nextLine().split(" "))
.mapToInt(Integer::parseInt).toArray();
System.out.println(Arrays.toString(st));
}
}
}
下面是输入和输出。我不知道为什么没有显示 [10, 0, 5] 。如果我按 Enter 键,则会出现 [10, 0, 5]。
Input
2
2
0 0 1
1 0 1
3
0 0 1
2 0 1
10 0 5
Output
[0, 0, 1]
[1, 0, 1]
[0, 0, 1]
[2, 0, 1]
(additional enter)
[10, 0, 5]
最佳答案
阅读java doc对于 nextLine
方法。
Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.
此 nextLine 调用会阻塞,直到找到行分隔符,即附加输入。
int[] st = Arrays.stream(sc.nextLine().split(" "))
.mapToInt(Integer::parseInt).toArray();
关于Java Scanner nextLine 问题,需要额外输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51702985/