2016-03-29 14 views
0

からデータを取得する:は、try /キャッチInputMismatch例外で苦労し、私は次のことをやろうとしている配列

  • 100ランダムに生成された整数でArrayを作成するメソッドを呼び出します。
  • 配列のインデックス位置を入力するようにユーザーに求め、入力された値を返すメソッドを呼び出します。ユーザーが入力した値が有効な整数でない場合は、ユーザーに問題のプロンプトを表示し、正しい整数を入力するか、プログラムを終了する機会を与える「入力ミスマッチ例外」を投げます。

package latest; 
import java.util.Scanner; 
import java.util.InputMismatchException; 
import java.util.Random; 

public class Latest 
{ 
    private static int[] randomInteger; 

    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     int indexPosition = 0;  
     randomInteger = new int[100]; 
     Random rand = new Random(); 
     for (int i=0; i<randomInteger.length;i++)   
      randomInteger[i] = rand.nextInt(); 

     while (indexPosition < 0 || indexPosition > 100) 
     {   
      try 
      { 
       // get array index position 
       System.out.println ("Hello, please enter an integer for the array index position: "); 
       indexPosition = input.nextInt(); 
      } 
      catch (InputMismatchException e) 
      { 
       System.out.println ("You did not input a valid value. Please enter an Integer value between 0 and 100"); 
       indexPosition = input.nextInt(); 
      } 
      {  
       System.out.println ("You did not input a valid value. Please enter an Integer value between 0 and 100"); 
       indexPosition = input.nextInt(); 
       System.out.println (randomInteger[indexPosition]); 
      } 
     } 
    } 
} 

私の問題は、コードがコンパイルされますが、何も出力し、IDEが言っているindexPosition - "Assigned value never used"

編集しないです:あなたはしかし、それを有効な整数を入力するとコメントでイングリッドのコードは完璧に動作例外が発生してもクラッシュすることはありません

+0

あなたのインデントを修正してください。.. – 3kings

+0

は、彼らがindexPosition'が開始時や、これまでその場合の '100' < 0 || >ない'ので、ファンキーであるように見えるあなたの 'しばらく()'ループの条件を確認してください。ループを終了するには、while(indexPosition!= -1) 'のようにする必要があります。 – 3kings

+0

負の数は、' while'の代わりに 'if'でなければならないのかどうか分かりません。私が必要とするのは、ユーザーが「0.5」や「文字列」を入力してプログラムをキャッチし、別の整数を尋ねる場合、または配列のインデックスに値を出力するために適切な整数を入力する場合です。 – archer

答えて

3

あなたはほとんどそこにいました。次のバージョンをお試しください。

package latest; 

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

public class Latest { 

private static int[] randomInteger; 

public static void main(String[] args) { 
    int indexPosition = 0; 
    Scanner input = new Scanner(System.in); 
    randomInteger = new int[100]; 
    Random rand = new Random(); 
    for (int i = 0; i < randomInteger.length; i++) 

     randomInteger[i] = rand.nextInt(); 

    System.out 
      .println("Hello, please enter an integer for the array index position: "); 
    do { 
     try { 
      // get array index position 
      indexPosition = input.nextInt(); 
     } catch (InputMismatchException e) { 
      System.out 
        .println("You did not input a valid value. Please enter an Integer value between 0 and 100"); 
      input.nextLine(); 
      continue; 
     } catch (Exception e) { 
      System.out 
        .println("You did not input a valid value. Please enter an Integer value between 0 and 100"); 
      input.nextLine(); 
      continue; 
     } 
     if (indexPosition < 0 || indexPosition > 100) { 
      System.out 
        .println("You did not input a valid value. Please enter an Integer value between 0 and 100"); 
     } 

    } while (indexPosition < 0 || indexPosition > 100); 
    System.out.println(randomInteger[indexPosition]); 
    input.close(); 
} 
} 
+0

有効な整数を入力しても、例外が発生しない場合は機能します – archer

+0

@archer私は持っています – alphablue

+0

コードにはまだいくつかの問題がありましたが、それ以上の調査をして自分自身で把握することができました。 – archer

関連する問題