亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java Scanner nextLine 問題,需要額外輸入

Java Scanner nextLine 問題,需要額外輸入

墨色風雨 2021-06-18 18:04:30
我正在學習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();


查看完整回答
反對 回復 2021-06-30
  • 2 回答
  • 0 關注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號