import java.util.*;
public class June16{
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
double b=0;
boolean checkInput = true;
do{
try{
System.out.println("Input b : ");
b = kb.nextDouble();
checkInput = false;
}catch(InputMismatchException ime){
}
}while(checkInput);
}
}
抛出InputMismatchException后,为什么我的程序不提示输入? :D
最佳答案
来自documentation :
When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.
这就是为什么如果您不输入有效的 double ,您最终会陷入无限循环。处理异常时,使用 kb.next()
移至下一个标记。
关于java.util.Scanner : why my nextDouble() does not prompt?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6374623/