-1

最近、このコードについて質問しましたが、別のコードがありました。テストで文字を入力すると、次のエラーが発生するため、整数のみを受け入れるようにスキャナーをセットアップします。

 java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at RockPaperScissorsTest.main(RockPaperScissorsTest.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

では、スキャナーで整数のみを許可するにはどうすればよいでしょうか? そのため、1 つ入力しても、スキャナー ボックスには何も表示されません。助けてくれてありがとう!以下、プログラム全体です。

 import java.util.Scanner;

 public class RockPaperScissorsTest {

     public static void main(String[] args) throws Exception {

    Scanner input = new Scanner(System. in );
    int P1;
    int P2;
    do {
        System.out.println("Player 1, choose 1 for rock, 2 for paper, or 3 for scissors.");
        P1 = input.nextInt();
    } while (P1 != 1 && P1 != 2 && P1 != 3);
    System.out.println("");
    System.out.println("");
    System.out.println("");
    do {
        System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
        P2 = input.nextInt();
    } while (P2 != 1 && P2 != 2 && P2 != 3);
    if (P1 == 1 & P2 == 1)
        System.out.println("It's a tie!");
    if (P1 == 1 & P2 == 2)
        System.out.println("Player 2 wins!");
    if (P1 == 1 & P2 == 3)
        System.out.println("Player 1 wins!");
    if (P1 == 2 & P2 == 1)
        System.out.println("Player 1 wins!");
    if (P1 == 2 & P2 == 2)
        System.out.println("It's a tie!");
    if (P1 == 2 & P2 == 3)
        System.out.println("Player 2 wins!");
    if (P1 == 3 & P2 == 1)
        System.out.println("Player 2 wins!");
    if (P1 == 3 & P2 == 2)
        System.out.println("Player 1 wins");
    if (P1 == 3 & P2 == 3)
        System.out.println("It's a tie!");
}

}

4

4 に答える 4

1

いくつかのオプションがあります。

  • Scanner.hasNextInt()整数を読み取れるかどうかを確認するために使用します。next()「スタック」しないように、トークンが整数でない場合はトークンをスキップする必要があることに注意してください。
  • トークンを文字列として読み取り、Integer.parseInt()整数として解析できないトークンを無視して を使用します。
于 2013-11-05T22:01:57.543 に答える
1

解決策は、次のように、while ブロックを使用して、入力が整数になるまでテストすることだと思います。

int choice;
Scanner sc = new Scanner(System.in);
String input = "a";
boolean notAnInteger = true;
while(notAnInteger){
     input = sc.next();
     try{
         choice = Integer.parseInt(input);
         notAnInteger = false;
     }catch(Exception e){
         System.out.println("Not an integer");
     }

}

私はこれをテストしていませんが、うまくいくはずです;)

于 2013-11-05T22:02:13.823 に答える
1
     boolean testing = false;
     String pos = "";
     while(true)
     {
     testing = false;   
     Scanner sc = new Scanner(System.in);
     System.out.println("Enter the integer value.... ");
     pos = sc.next();
     for(int i=0; i<pos.length();i++)
     {
         if(!Character.isDigit(pos.charAt(i)))
             testing = true;
     }
     if(testing == true)
     {
         System.out.print("Enter only numbers..   ");
         continue;
     }

     else
     {
         int key = Integer.parseInt(pos);
         // Your code here....
         break;
     }
于 2014-08-13T11:34:42.340 に答える
0

ここにはいくつかのオプションがあります

  1. try/catch ブロック内で input.nextInt() を囲み、InputMismatchException をキャッチできます。その後、もう一度プロンプトを表示したり、好きなように処理したりできます。

    try {
       P1 = input.nextInt();
    } catch (InputMismatchException e) {
       //Handle how you choose.
    }
    
  2. Scannerには、次のトークンが int の場合に true と評価される hasNextInt() メソッドがあります。

    if(input.hasNextInt()){
       P1 = input.nextInt();
    } else {
       //Handle how you choose. 
    } 
    
于 2013-11-05T22:11:00.633 に答える