2017-11-13 21 views
0

これは宿題なので、いくつかのガイダンスを探しています。ユーザーが指定したインデックス値に基づいて配列内の値を返す作業をしています。無効な値の特定の条件と例外処理でループを終了する方法

「99」が入力されたときにプログラムを停止する必要があるのは、try-catch IndexOutOfRangeExceptionをトリガーする値です。 "99"と他の範囲外の値との差異をプログラムが認識することはできません。私はif-elseで試してみましたが、 "99"はまだIndexOutOfRangeExceptionをスローしてループしませんでした。 do-whileの試みもうまくいかなかった。

コードは以下のとおりです。前もって感謝します。

using System; 
using static System.Console; 

class SubscriptExceptionTest 
{ 
static void Main() 
{ 
    double[] array = {20.3, 44.6, 32.5, 46.7, 89.6, 67.5, 12.3, 14.6, 22.1, 13.6}; 

    int i = 0; 

    while (i != 99) 
     try 
     { 

      Write("Enter a number to see the value in that position. Type 99 to stop: "); 
      i = Convert.ToInt32(ReadLine()); 

      double arrayVal = array[i]; 
      WriteLine("The value at index {0} is {1}", i, arrayVal); 
      ReadLine(); 
     } 
     catch (FormatException fe) 
     { 
      throw new FormatException("You did not enter an integer."); 
     } 
     catch (IndexOutOfRangeException ie) 
     { 
      throw new IndexOutOfRangeException("Index was outside the bounds of the array."); 
     } 
     catch (Exception ex) 
     { 
      throw new Exception("Error: " + ex.Message); 
     } 
    } 
} 

答えて

0

あなたは再びコードのどこに扱われていないcatchブロックの内側にそれを投げているIndexOutOfRangeExceptionをキャッチされると問題があります。

もう1つの問題は、Exceptionにつながる配列でアクセスするために使用する前に、iの値が99であることを確認していないことです。

は、下記を参照してください -

int i = 0; 

while (i != 99) 
    try 
       { 

        Write("Enter a number to see the value in that position. Type 99 to stop: "); 
        i = Convert.ToInt32(ReadLine()); 
        if(i == 99) { 
         Console.WriteLine("Thanks!!, Breaking here!!!"); 
         break; 
        } 
        double arrayVal = array[i]; 
        WriteLine("The value at index {0} is {1}", i, arrayVal); 
        ReadLine(); 
       } 

       catch (FormatException fe) 
       { 
        throw new FormatException("You did not enter an integer."); 
       } 

       catch (IndexOutOfRangeException ie) 
       { 
        // Show some message here 
       } 

} 
+0

評価のソフトウェアはまだif文に載っていますが、Visual Studioでは動作しますが、それで十分です。ありがとうございました! ifステートメントの配置が問題でした。時には、この人間がコンピュータのように考えるのは本当に難しいです。 –

0

プットANを構築物は値99をチェックし、条件がtrueの場合、アプリケーションを終了している場合。原則として

i = Convert.ToInt32(ReadLine()); 

if(i == 99) 
{ 
    //Perform any operation you want to before exiting the application. 
    Console.WriteLine("Exiting Application"); 

    //This will terminate the program. 
    Environment.Exit(0); 
} 
0

、/キャッチのと例外ハンドラを試してみて例外的のみを使用する必要があります。それらは高価です。防衛プログラミングを使ってこれらの問題を処理する方法を教えてください。

他の範囲外の値が希望の答えである場合は、Array.Lengthを使用して配列の境界を確認すると思います。

static void Main() 
{ 
double[] array = {20.3, 44.6, 32.5, 46.7, 89.6,67.5, 12.3, 14.6, 22.1, 13.6}; 

int i = 0; 

while (i != 99) 
{ 
     Write("Enter a number to see the value in that position. Type 99 to stop: "); 

     var result = ReadLine(); 

     bool isInteger = int.TryParse(result, out i); 

     if (isInteger == false) throw new FormatException("You did not enter an integer."); 

     if (i == 99) Environment.Exit(0); 

     if (i < 0 || i > array.Length) throw new IndexOutOfRangeException("Index was outside the bounds of the array."); 

     double arrayVal = array[i]; 

     WriteLine("The value at index {0} is {1}", i, arrayVal); 

     ReadLine();  
} 
+0

これは本当に役に立ちます。学生プログラマとして、私は課題の要件を満たさなければなりませんが、「実世界」のシナリオでこのようなことを実証する時間をとっていただき、大変感謝しています。 –

+0

問題を予期し、骨の後の犬のようなバグの後に行って、デバッグをうまくやってください!最高のアドバイス、いくつかの本を読んでください。チュートリアルとチューターでそれを褒める。がんばろう!! –

関連する問題