0
int i;
Scanner scan = new Scanner(System.in) {
i = scan.nextInt();
}
我想要做的是在用户输入字符而不是整数时捕获扫描仪中的错误。我尝试下面的代码,但结束调用为另一个用户输入(BEC验证的唯一数字第一scan.nextInt()之后调用至i分配值另一个scan.nextInt()的。):在分配给int变量前检查用户输入(扫描仪)
int i;
Scanner scan = new Scanner(System.in) {
while (scan.hasNextInt()){
i = scan.nextInt();
} else {
System.out.println("Invalid input!");
}
}
我有一个错误使用“while(scan.hasNext()谢谢! – 2014-11-07 02:36:31