次のコードはwhileループ内にあり、スイッチをそのまま格納します。
System.out.println("Enter in a selection.");
System.out.println("Enter \"1\" for a default selection of die");
System.out.println("Enter \"2\" for a custom number of sides.");
//try the input to see if its an integer
try {
selection = sc.nextInt();
} catch (NumberFormatException e){
System.out.print("Your selection can only be an integer!");
}
switch (selection){
case1:
...
break;
case2:
...
break;
default:
...
//yell at them
continue;
}
私はすでにスイッチにデフォルトの選択を持っているので、ユーザーが4のような無効な番号を入力すると(2つのケースしかないため)、ループの最初に戻ります。したがって、問題は例外の処理です。例外は上記のコードでは処理されません。理由はわかりません。試みは、問題のあるコードを収容することです。
いつものように、必要に応じて説明を求めてください。ありがとう。