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

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

java中如何在一行中輸入4個不同Int的數字?

java中如何在一行中輸入4個不同Int的數字?

達令說 2024-01-28 17:04:55
問題就是這么簡單。我想,就像Python一樣,找到一種方法,幫助我在一行中輸入4個具有不同Int的數字。我的意思是在java中,我想要,輸入是:1911輸出是:年:19月:11
查看完整描述

1 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

嘗試這個


try {

    Scanner io = new Scanner(System.in);

    String input = io.next();

    // Note StringIndexOutOfBoundsException

    int year = Integer.parseInt(input.substring(0, 2));

    int month = Integer.parseInt(input.substring(2, 4));

    System.out.println("year: " +  year + "month: " +  month);

} catch (Exception e) {

    e.printStackTrace();

}

測試


1911

輸出


year = 19, month = 11

或者


Scanner io = new Scanner(System.in);

String year = io.next(), month = io.next();

System.out.println("year: " +  year + "month: " +  month);

測試


19 11

輸出


year = 19, month = 11


查看完整回答
反對 回復 2024-01-28
  • 1 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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