2016-04-26 19 views
0

アルファベットやその他の記号なしで、80〜120の間の整数を入力したいと思っています。ここに私のコードです:ユーザが2つの数字の間の整数を入力するかどうかを確認するにはどうすればよいですか?

import java.util.*; 

public class Test {  

public static void main(String[] args) 
{  
    Scanner in = new Scanner(System.in); 

//checking for integer input 
    while (!in.hasNextInt()) 
    { 
     System.out.println("Please enter integers between 80 and 120."); 
     in.nextInt(); 
     int userInput = in.nextInt(); 

//checking if it's within desired range 
     while (userInput<80 || userInput>120) 
     { 
      System.out.println("Please enter integers between 80 and 120."); 
      in.nextInt(); 
     } 
    } 
} 

}

しかし、私はエラーに直面しています。これには解決策がありますか?

Exception in thread "main" 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 Array.main(Array.java:15) 

ありがとうございました! :)

EDITは:あなたはトムをありがとう、解決策を得たが、あなたのループ条件が間違っている

Scanner in = new Scanner(System.in); 

    int userInput; 
    do { 
    System.out.println("Please enter integers between 80 and 120."); 
    while (!in.hasNextInt()) 
    { 
     System.out.println("That's not an integer!"); 
     in.next(); 
     } 
     userInput = in.nextInt(); 
} while (userInput<81 || userInput >121); 

System.out.println("Thank you, you have entered: " + userInput); 
} 
} 
+0

確かにあります! 'java.util.InputMismatchException'を' catch 'し、適切に処理します。興味のないところで、なぜ入力をスキップしていますか? – Bathsheba

+0

@Bathshebaこんにちは、私は学校の仕事を改訂しているのでキャッチを使わずにやりたいと思っています。これはちょうど最初のいくつかの話題です(この時点でキャッチ例外は学ばれていません) – Ken

+0

while (in.hasNextInt()) – Unknown

答えて

0

「を行う」せずに試してみたいと思います。 「入力から利用可能な整数がない限り、整数を読み込む」のようなものをチェックします。それは失敗するつもりです

また:nextIntを呼び出しています。それをしないでください。

System.out.println("Please enter integers between 80 and 120."); 
in.nextInt(); //REMOVE THIS LINE 
int userInput = in.nextInt(); 

intはhasNextIntを使用して利用可能である場合は、一度チェックしているが、その後、あなたは二度値を読み取る:最初のコールを削除します!このコードで

+0

私は試しましたが、同じエラーが発生しています。 – Ken

+0

うん、私はそれを削除しようとしていると私はまだ同じエラーが発生しましたD: – Ken

+1

'while(!in.hasNextInt())'おそらくあなたの問題はここにあります。 –

0
boolean continueOuter = true; 

    while (continueOuter) 
     { 
      System.out.println("Please enter integers between 80 and 120."); 
      String InputVal = in.next(); 

     try { 
      int input = Integer.parseInt(InputVal); 
       //checking if it's within desired range 
      if(FirstInput>80 && FirstInput<120) 
      { 
       //no need to continue got the output 
       continueOuter = false; 
      }else{ 
        System.out.println("Please enter integers between 80 and 120."); //need to continue didnt got the exact output 
       continueOuter = true;  
      } 

     } catch (Exception e) { 
     //not an int  
     System.out.println(InputVal); 
     continueOuter = true; 
     } 
} 

私はプログラムを継続するかしない望んでいるかどうかをチェックするブール値を作成しました。ユーザーが有効な値を入力した場合、プログラムは停止しますが、希望どおりに変更することができます。 あなたは2つのwhileループを必要としませんwhileループwhileループを変更しました私のコードを見てください

+1

'continue = false'? Javaでそれと幸運。 – Bathsheba

+0

私はそれを続けることを言いました、それは間違いでした。 – Priyamal

+0

@priyamalこんにちは、ユーザーが文字を入力するとどうなりますか?私は頭の文字は数字か何かを指します。 – Ken

関連する問題