我正在學習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] 沒有顯示。如果我按下回車鍵,則會出現 [10, 0, 5]。Input220 0 11 0 130 0 12 0 110 0 5Output[0, 0, 1][1, 0, 1][0, 0, 1][2, 0, 1](additional enter)[10, 0, 5]
2 回答
慕無忌1623718
TA貢獻1744條經驗 獲得超4個贊
閱讀Java文檔為nextLine方法。
由于此方法繼續搜索輸入以尋找行分隔符,因此如果不存在行分隔符,它可能會緩沖所有搜索要跳過的行的輸入。
這個 nextLine 調用會阻塞,直到它找到行分隔符,這是額外的輸入。
int[] st = Arrays.stream(sc.nextLine().split(" "))
.mapToInt(Integer::parseInt).toArray();添加回答
舉報
0/150
提交
取消
