扫描器类如何适用于String“1 a 10. 100 sfn 1000”

时间:2018-04-29 08:56:28

标签: java

我在采访中被问到这个节目问题。

String test = "1 a 10 . 100 sfn 1000";
Scanner scan = new Scanner(test);
int total = 0;
for(int i = 0; i < 4; i++) {
    total = total + scan.nextInt(); //not sure if this was the exact line but something like this
}
System.out.println(total);

1 个答案:

答案 0 :(得分:-1)

我尝试了替代品,在这里我得到了答案 It will scan first int and print it in console after which exception will be thrown

所以在我的情况下,我有4个选择,1,11,111和1111,所以答案是1。