9

ユーザー入力値が int 値でないかどうかを確認する必要があります。私が知っていることのさまざまな組み合わせを試しましたが、何も得られないか、ランダムなエラーが発生します

例えば:

ユーザーが「adfadf 1324」と入力すると、警告メッセージが表示されます。


私が持っているもの:

       // Initialize a Scanner to read input from the command line
       Scanner sc = new Scanner(System.in);
       int integer, smallest = 0, input;
       boolean error = false;

       System.out.print("Enter an integer between 1-100: ");
       range = sc.nextInt();

       if(!sc.hasNextInt()) {

          error = true;
          System.out.println("Invalid input!");
          System.out.print("How many integers shall we compare? (Enter an integer between 1-100: ");
          sc.next();
    }

       while(error) {
          for(int ii = 1; ii <= integer; ii++) {

              ...

          } // end for loop
      }
      System.out.println("The smallest number entered was: " + smallest);

      }
  }
4

7 に答える 7

19

入力が無効な場合は、単純に例外をスローします

Scanner sc=new Scanner(System.in);
try
{
  System.out.println("Please input an integer");
  //nextInt will throw InputMismatchException
  //if the next token does not match the Integer
  //regular expression, or is out of range
  int usrInput=sc.nextInt();
}
catch(InputMismatchException exception)
{
  //Print "This is not an integer"
  //when user put other than integer
  System.out.println("This is not an integer");
}
于 2013-08-08T06:27:44.357 に答える
6
Try this one:

    for (;;) {
        if (!sc.hasNextInt()) {
            System.out.println(" enter only integers!: ");
            sc.next(); // discard
            continue;
        }
        choose = sc.nextInt();
        if (choose >= 0) {
            System.out.print("no problem with input");

        } else {
            System.out.print("invalid inputs");

        }
    break;
  }
于 2013-08-08T06:39:48.807 に答える
2

次のエラーがあり、それが原因でその例外が発生しています。説明させてください

これはあなたの既存のコードです:

if(!scan.hasNextInt()) {
        System.out.println("Invalid input!");
        System.out.print("Enter an integer: ");
        usrInput= sc.nextInt();
    }

上記のコードでは、ユーザー入力に文字と入力のような整数の両方が含まれている場合にのみに if(!scan.hasNextInt())なります。trueadfd 123

ただし、を使用して if 条件内の整数のみを読み取ろうとしています usrInput= sc.nextInt();。どちらが間違っていますか、それが投げているものですException in thread "main" java.util.InputMismatchException

したがって、正しいコードは

 if(!scan.hasNextInt()) {
            System.out.println("Invalid input!");
            System.out.print("Enter an integer: ");
            sc.next(); 
            continue;
        }

上記のコードでsc.next()は、ユーザーからの新しい入力を読み取る のcontinueに役立ち、同じ if condition( i.e if(!scan.hasNextInt())) を再度実行するのに役立ちます。

私の最初の回答のコードを使用して、完全なロジックを構築してください。説明が必要な場合はお知らせください。

于 2013-08-08T07:32:36.170 に答える
0

関連する投稿から取得:

public static boolean isInteger(String s) {
    try { 
        Integer.parseInt(s); 
    } catch(NumberFormatException e) { 
        return false; 
    }
    // only got here if we didn't return false
    return true;
}
于 2013-08-08T06:32:10.533 に答える
0

多分あなたはこれを試すことができます:

int function(){
Scanner input = new Scanner(System.in);   
System.out.print("Enter an integer between 1-100: ");   
int range;
while(true){   
    if(input.hasNextInt()){   
    range = input.nextInt();
    if(0<=range && range <= 100)
        break;
    else
        continue;
    }
    input.nextLine();  //Comsume the garbage value
    System.out.println("Enter an integer between 1-100:");
}
return range;
}
于 2015-03-26T07:23:26.353 に答える
0

これは、この入力が整数である間、入力を要求し続け、それが奇数か偶数かを見つけて終了するためです。

int counter = 1;
    System.out.println("Enter a number:");
    Scanner OddInput = new Scanner(System.in);
        while(OddInput.hasNextInt()){
            int Num = OddInput.nextInt();
            if (Num %2==0){
                System.out.println("Number " + Num + " is Even");
                System.out.println("Enter a number:");
            }
            else {
                System.out.println("Number " + Num + " is Odd");
                System.out.println("Enter a number:");
                }
            }
        System.out.println("Program Ended");
    }
于 2015-11-11T09:01:38.937 に答える