我需要獲取多行輸入,這些輸入將來自控制檯的整數,用於處理類問題。到目前爲止,我一直在使用掃描儀,但我沒有解決方案。輸入包括n線的數量。輸入以一個整數後跟一串整數開始,這個重複很多次。當用戶輸入0即輸入停止時。在java中從控制檯讀取多行
例如
輸入:
3
3 2 1
4
4 2 1 3
0
那麼,如何可以讀此一系列的線和可能的存儲每行使用掃描儀對象的陣列的元件?到目前爲止,我曾嘗試:
Scanner scan = new Scanner(System.in);
//while(scan.nextInt() != 0)
int counter = 0;
String[] input = new String[10];
while(scan.nextInt() != 0)
{
input[counter] = scan.nextLine();
counter++;
}
System.out.println(Arrays.toString(input));
您正在運行到這個[跳過問題](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) –
你需要2個循環:一個外部循環讀取數量,和一個內部循環讀取許多整數。在兩個循環結束時,你需要'readLine()' – Bohemian