1

次のコードは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つのケースしかないため)、ループの最初に戻ります。したがって、問題は例外の処理です。例外は上記のコードでは処理されません。理由はわかりません。試みは、問題のあるコードを収容することです。

いつものように、必要に応じて説明を求めてください。ありがとう。

4

6 に答える 6

1

例外が発生した場合はcontinue、先に進む意味がないため、例外をオンにする必要があります。switchselection

} catch (InputMismatchException e){
    System.out.print("Your selection can only be an integer!");
    sc.nextLine();
    continue;
}

現在の方法では、例外が発生した場合は、によって保持されている古い古い値を切り替えることになりますselection


ただし、コードの主な問題は、nextInt()メソッドがをスローせずNumberFormatException、代わりにをスローすることInputMismatchExceptionです。

したがって、間違った例外をキャッチしようとしています。

于 2012-11-04T01:56:23.040 に答える
1

入力時に例外が処理されない理由44、有効な整数であり、例外nextInt()が発生しないためNumberFormatExceptionです。

をブロックswitchで囲んだほうがいいです。try

于 2012-11-04T01:59:38.720 に答える
1

個人的には、入力 (およびそれに付随する try/catch ブロック) を独自の別のメソッドに入れます。ブール値 (true = 有効な整数) または範囲外の値 (プログラムによっては "-1" など) を返します。

私見では...

于 2012-11-04T02:01:16.540 に答える
1

Scanner#nextInt()がスローされないNumberFormatException

Scans the next token of the input as an int. An invocation of this method of the form nextInt() behaves in exactly the same way as the invocation nextInt(radix) , where radix< is the default radix of this scanner.

 return the int scanned from the input
 throws InputMismatchException
         if the next token does not match the <i>Integer</i>
          regular expression, or is out of range
 throws NoSuchElementException if input is exhausted
 throws IllegalStateException if this scanner is closed

例外処理は正しいようですが、間違った例外をキャッチしているという問題のみがあります。他の2つをキャッチInputMismatchExceptionしてください。

以下に例を示します。

    try {
          selection = sc.nextInt();
      } catch (InputMismatchException e){
          System.out.print("Your selection can only be an integer!");
      }

また、上記のコードを以下のように while ループに入れることもできます。

       boolean validInput = false;
       while(!validInput){
     try {
          selection = sc.nextInt();
              validInput = true;
      } catch (InputMismatchException e){
          System.out.print("Your selection can only be an integer!");
      }
       }

これは、数字が入力されるまで繰り返し入力を求めます。

于 2012-11-04T02:19:09.633 に答える
0

の場合、例外を介してクラッシュして書き込むのではなく、メソッド (および他のデータ型の同等のもの) をScanner使用できます。hasNextInt()

while (some_condition) {
  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
  if (!sc.hasNextInt()) {
    System.out.println("You must enter an integer!");
    continue;
  }
  selection = sc.nextInt();

  switch (selection){
   case 1:
   ...
   break;

   case 2:
   ...
   break;

   default:
   ...
   //yell at them
   continue;
  }
}

Bhesh Gurung が言ったように、ループに陥っている場合は、この場合continueのように、 を使用して最初に戻る必要がありますdefault

于 2012-11-04T02:00:09.067 に答える
0

「switch」ステートメントで実際に例外を処理したい場合は、try-catch ブロックのスコープを拡張する必要があります。

  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();

  switch (selection){
   case1:
   ...
   break;

   case2:
   ...
   break;

   default:
   ...
   //yell at them
   throw new NumberFormatException("Yelling message");
   continue;
  }
 } catch (NumberFormatException e){
      System.out.print("Your selection can only be an integer!");
 } 
于 2012-11-04T02:12:21.243 に答える