問題就是這么簡單。我想,就像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
添加回答
舉報
0/150
提交
取消