2017-10-21 15 views
0

ユーザがintを入力し、範囲が10〜20であることを確認しようとしています
範囲外のintを最初に入力するとエラーになります文字列を入力します。数値が指定された範囲に収まるまで入力を続けるようにユーザーに依頼し続ける方法についてはわかりません。 ご協力いただければ幸いです!ユーザ入力の可能性すべて

public static void main(String[] args) { 

    Scanner scnr = new Scanner(System.in); 

    int width = 0; 
    int height = 0; 

    System.out.println("Welcome to Mine Sweeper!"); 
    System.out.println("What width of map would you like (10 - 20): "); 
    //width = scnr.nextInt() 
    while (!scnr.hasNextInt()) { 
     System.out.println("Expected a number from 10 to 20"); 
     scnr.next(); 
    } 
    do { 
     width = scnr.nextInt(); 
     if (width < 10 || width > 20) { 
      System.out.println("Expected a number from 10 to 20"); 
      //width = scnr.nextInt(); 
     } 
    } while (width < 10 || width > 20); 
} 
+0

エラーを投稿してください! – phpdroid

答えて

0

これを試してみてください。これはあなたが望む何をすべき

Scanner scnr = new Scanner(System.in); 

    int width = 0; 
    int height = 0; 

    System.out.println("Welcome to Mine Sweeper!"); 
    while(true) 
    { 
     System.out.println("What width of map would you like (10 - 20): "); 
     try{ 
      width = scnr.nextInt(); 
      System.out.println(width); 
      if (width < 10 || width > 20) 
      { 
       System.out.println("Expected a number from 10 to 20"); 
      } 
      else 
       break; 
     }catch(Exception e) 
     { 
      System.out.println("Expected a number from 10 to 20"); 
      scnr.next(); 
     } 
    } 
    scnr.close(); 
+0

tryとcatchを使用せずにこれを行う方法はありますか? – JRob

+0

@JRob 'try/catch'を使わないと、' int'の代わりに 'String'を入力したときに例外をどのように処理しますか? 更新されたコードも確認してください。 –

0

while (scnr.hasNext()) { 
    if (scnr.hasNextInt()) { 
     width = scnr.nextInt(); 
     if (width >= 10 && width <= 20) { 
      break; 
     } 
    }else{ 
     scnr.next(); 
    } 
    System.out.println("Expected a number from 10 to 20"); 
} 
System.out.println("width = " + width); 
0

私の理解では、あなただけの範囲の整数値を受け入れたいと思っています10および20を含む。文字または文字列が入力されている場合は、メッセージを印刷して実行を続行します。もしそうなら、これはあなたが望むものに近いはずです。私はもともとtry-catchのステートメントを使用してこの質問に答えようとしていましたが、あなたは別の答えに答えて、try-catchを使用する以外の方法が必要だと気付きました。このコードは100%機能的ではありません。いくつかのことを微調整する必要がありますが、それはtry-catchステートメントを使用せずに私が考えると思っているものに非常に近いです。

public static void main(String args[]) 
{ 
    Scanner scanner = new Scanner(System.in); 
    int width = 0; 
    int height = 0; 
    int validInput = 0; //flag for valid input; 0 for false, 1 for true 

    System.out.println("Welcome to Mine Sweeper!"); 
    System.out.println("What width of map would you like (10 - 20): "); 

    while (validInput == 0) //while input is invalid (i.e. a character, or a value not in range 
    { 
     if (scanner.hasNextInt()) 
     { 
      width = scanner.nextInt(); 
      if (width >= 10 && width <= 20) 
      { 
       validInput = 1; //if input was valid 
      } 
      else 
      { 
       validInput = 0; //if input was invalid 
       System.out.println("Expected a number from 10 to 20."); 
      } 
     } 
     else 
     { 
      System.out.println("You must enter integer values only!"); 
      validInput = 0; //if input was invalid 
     } 
     scanner.next(); 
    } 
} 
関連する問題