私はこのコードtry/catchを使用すると無限ループを回避するにはどうすればいいですか?
public void askUserForStrategy(){
try{
System.out.println("What strategy do you want to use?\n");
System.out.println("1 = Math.random\t 2 = System time\t "
+ "3 = Sum of Math.random and System time");
int strategy = sc.nextInt();
selectStrategy(strategy);
}
catch(InputMismatchException Exception){
System.out.println("That is not an integer. Try again.\n");
askUserForStrategy();
}
}
私はそれがやりたいことは基本的に整数を入力するようユーザーに依頼して、ユーザのユーザタイプ例えば文字列の場合には、その例外をキャッチし、方法を開始しているがありますもう一度(整数値を入力するようユーザーに依頼する)。しかし、このメソッドは、ユーザーがString型を入力するとループします。
ドゥプログラムの流れに 'try-catch'を使わないでください。回復不能な例外のみを処理する必要があります。同じプロセスを再試行しないでください。 – Filburt