所以我在这里写代码只是为了好玩,但我遇到了一个我似乎无法修复的错误。这段代码应该接受一个 int ......起初我在 while 循环中单独使用 hasNextInt() 来尝试确保我得到正确的输入,但命运会这样......我得到了例外。然后我添加了一个尝试捕获它想也许我做错了什么......但我仍然得到同样的错误。我不知道这里有什么问题。这实际上是我第一次使用 try catch 块(仍然是一个菜鸟)。它对我来说看起来不错,我查看了在线文档并进行了一些小型研究,但无济于事。谁能确定这里有什么问题?一探究竟:
do{
System.out.println("How much AP do you want to allocate towards HP? ");
try {//added try catch... still throwing the exception..
while(!in.hasNextInt()){//this should've been enough, apparently not
System.out.println("That is not a valid input, try again.");
in.nextInt();
}
} catch (InputMismatchException e) {
System.out.print(e.getMessage()); //trying to find specific reason.
}
hpInput = in.nextInt();
}while(hpInput < 0 || hpInput > AP);
如果我输入了一个字符串,它会给我“这不是一个有效的输入,再试一次”。行..但异常仍然会在之后发生,而不是仅仅循环直到检测到实际的 int ......帮助 plz..