简体   繁体   English

如何在 java 中仅在一行中输入具有不同 Int 的 4 个数字?

[英]How to input 4 Numbers in just one line with different Int in java?

The problem is so simple.问题是如此简单。 I want to, just like Python, find a method, that helps me to input 4 numbers with different Int just in One line.我想,就像 Python 一样,找到一种方法,可以帮助我在一行中输入 4 个具有不同 Int 的数字。 I mean in java, I want to, The input is: 1911 The out put is: Year:19 Month:11我的意思是在 java 中,我想,输入是:1911 输出是:年:19 月:11

Try this尝试这个

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();
}

Test测试

1911

Output Output

year = 19, month = 11

Or或者

Scanner io = new Scanner(System.in);
String year = io.next(), month = io.next();
System.out.println("year: " +  year + "month: " +  month);

Test测试

19 11

Output Output

year = 19, month = 11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM