java.util.Scanner:为什么我的nextDouble()不提示? [英] java.util.Scanner : why my nextDouble() does not prompt?
本文介绍了java.util.Scanner:为什么我的nextDouble()不提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
After InputMismatchException is thrown, why my program not prompt for input? :D
推荐答案
来自文档:
当扫描程序引发InputMismatchException时,扫描程序将不会传递导致异常的令牌,因此可以通过其他方法检索或跳过该令牌.
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()
移至下一个标记.
This is why you end up in an infinite loop if you don't enter a valid double. When you handle the exception, move to the next token with kb.next()
.
这篇关于java.util.Scanner:为什么我的nextDouble()不提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文