扫描程序类如何适用于字符串"1 a 10 . 100 sfn 1000"



我在面试中被问到这个程序问题。

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

我尝试了替代方案,这里有答案 它将首先扫描 int 并在控制台中打印它,之后将引发异常

所以就我而言,我有 4 个选择,1、11、111 和 1111,所以答案是 1。

最新更新