2016-03-29 3 views
0
package test5555; 
import java.util.InputMismatchException; 
import java.util.Random; 
import java.util.Scanner; 

public class Test5555 { 

private static int[] randomInteger; 

public static void main(String[] args) { 

boolean validInput = false; 
randomInteger = new int[100]; 
Random rand = new Random(); 
for (int i = 0; i < randomInteger.length; i++) 
randomInteger[i] = rand.nextInt(); 
int indexPosition = 0; 

Scanner input = new Scanner(System.in); { 
System.out.println("Please enter an integer for the array index position: "); 

while(!validInput) 
{ 
try 
{ 

indexPosition = input.nextInt(); 
validInput = true; 

System.out.println(randomInteger[indexPosition]); 
} catch (InputMismatchException | IndexOutOfBoundsException ex) { 

System.out.print("Please enter a valid integer between 0 and 100 or type quit to exit: "); 

String s = input.next(); 
if(s.equals("quit")){ 
System.exit(0);  
System.out.println(randomInteger[indexPosition]); 
} 
} 
} 
} 
} 
} 
  • コードは完全に私は解決できない2つのマイナーしゃっくりを除いて動作します。あなたがそれを実行するときPlease enter an integer for the array index position:を取得します。bobのような100以上の文字列を入力すると、Please enter a valid integer between 0 and 100 or type quit to exit:が完璧です。しかし、quitと入力するとPlease enter a valid integer between 0 and 100 or type quit to exit: BUILD SUCCESSFUL (total time: 2 minutes 2 seconds)となり、終了しますが、不要な例外文が繰り返されます。あなたが正しい整数を入力した場合小さな問題 - ない正しい出力

  • あなたは100を超える数を入力してPlease enter a valid integer between 0 and 100 or type quit to exit:を受信するとプログラムがちょうどオフになり、それがBUILD SUCCESSFULの代わりに、配列

答えて

0

は、以下で

String s = null; 
while(!validInput) 
{ 
try 
{ 
if(s != null){ 
    indexPosition = Integer.parseInt(s); 
} 
else{ 
indexPosition = input.nextInt(); 
} 
System.out.println(randomInteger[indexPosition]); 
validInput = true; 
} catch (InputMismatchException | NumberFormatException | IndexOutOfBoundsException ex) { 

System.out.println("Please enter a valid integer between 0 and 100 or type quit to exit: "); 
input.nextLine(); 
s = input.next(); 
if(s.equals("quit")){ 
System.exit(0); 
} 
} 
} 

をごwhileループコードを交換して、スキャナの詳細アイデアを得るために、これをお読みください。 https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

あなたのケースでは問題は、(DOC 1として)である

スキャナがInputMismatchExceptionをスローする場合、それは を取得することができるように、スキャナは は、例外の原因となったトークンを通過しないであろうが他の方法でスキップすることもできます。

+0

これは完璧に働いてくれてありがとう – archer

0
からあなたのための番号を取得すると言うだろう
  • ポイント1で説明した動作は正しくありません。数字を入力して終了すると、「期待通りに」動作します
  • 「bob」などの文字列を入力すると、nextInt()が失敗し、InputMissmatchExceptionが発生し、「input.next()」コールが失敗します。 catch節は "bob"を読み、 "quit"と等しくないことを確認し、ループに戻ってブロックし、 "int"を待ちます。

    • インポイント2.あなたはint型を入力して例外を取得しますが、validInputをtrueに設定してループを終了します。印刷後にvalidInputを設定する必要があります。
0

私が正しくあなたの質問を得た場合、私は少し異なる実装します。あなたの要件を満たしているかどうか確認してください。

import java.util.Random; 
import java.util.Scanner; 

public class Test5555 { 

    private static int[] randomInteger; 

    public static void main(String[] args) { 

     randomInteger = new int[100]; 
     Random rand = new Random(); 
     int indexPosition; 

     for (int i = 0; i < randomInteger.length; i++) 
      randomInteger[i] = rand.nextInt(); 


     Scanner input = new Scanner(System.in); 
     System.out.println("Please enter an integer for the array index position: "); 

     while(true) { 
      String strIndex = input.next(); 
      if(strIndex.equals("quit")) break; 
      indexPosition = getIntVal(strIndex); 

      if(indexPosition < 0 || indexPosition >= randomInteger.length) { 
       System.out.print("Please enter a valid integer between 0 and " 
          + randomInteger.length + " or type quit to exit: "); 
       continue; 
      } 

      System.out.println(randomInteger[indexPosition]); 
      break; 
     } 

     input.close(); 
    } 

    protected static int getIntVal(String inputStr) { 
     int result = -1; 
     try { 
      result = Integer.parseInt(inputStr); 
     } catch(NumberFormatException e) {} 
     return result; 
    } 

} 
+0

これは完璧に動作し、私の要求を満たすものです – archer

0

あなたのコードのこの部分は間違っ

try 
{ 

indexPosition = input.nextInt(); 
validInput = true; 

System.out.println(randomInteger[indexPosition]); 
} catch (InputMismatchException | IndexOutOfBoundsException ex) { 

であるあなたは、場合

validInput = true; 

ラインは後でチェックすべきであり、それをチェックするために右の前に、あなたのindexPositionであることを言っています配列はその位置にあります。

右コード:

... 
indexPosition = input.nextInt(); 
System.out.println(randomInteger[indexPosition]); 
validInput = true; 
.... 
関連する問題