2016-04-02 10 views
0

ご意見ありがとうございます。私はまだ範囲外の例外を取得しています。私はデバッグしていますが、なぜ例外が発生しているのか理解できません。 intが特定の他のintよりも高い場合は動作するはずです。それは長さと関係がありますか?カウント?データの有効性を確認したC#リスト数

私はif文他に最初の検証を持ついくつかの問題を抱えている

    do 
       { 
        Console.Write("Input the element that you want to exclude from the list: "); 
        result = int.TryParse(Console.ReadLine(), out delSelection); 

        tempInputDelInt = (int)(tempList[delSelection]); 
        tempInputDelStr = Convert.ToString(tempInputDelInt); 

        if (result == true && tempInputDelInt >= 0 && tempInputDelInt < tempList.Count) 
        { 
         tempList.RemoveAt(delSelection); 
         Console.WriteLine("\nYou've deleted the temperature " + tempInputDelStr + " from index " + delSelection); 
         success = false; 
         Console.ReadKey(true); 
         Console.Clear(); 
        } 

        else if (result == true && tempInputDelInt >= tempList.Count || tempInputDelInt < 0) 
        { 
         Console.WriteLine("You've input a number that's outside the list. Input a digit between 0 and " + (tempList.Count - 1) + ".\n"); 
         success = true; 
         Console.ReadKey(true); 
        } 

        else if (result == false) 
        { 
         Console.WriteLine("\nYou didn't input a digit, try again!\n"); 
         success = true; 
         Console.ReadKey(true); 
        } 
       } while (success); 
       success = false; 

更新されたコード。配列の外側の値が入力されたときにケースをキャッチしたいが、私はそれを管理しない。

私はarray.Lengthと実質的に同じプログラムを成功させました。

array.Listとlist.Countに違いはありますか?それは問題ですか?

ご協力いただきありがとうございます。

    do 
       { 
        Console.Write("Input the element that you want to exclude from the list: "); 
        result = int.TryParse(Console.ReadLine(), out delSelection); 


        tempInputDel = Convert.ToString(tempList[delSelection]); 

        if (result == true && delSelection >= 0 && delSelection < tempList.Count) 
        { 
         tempList.RemoveAt(delSelection); 
         Console.WriteLine("\nYou've deleted the temperature " + tempInputDel + " from index " + delSelection); 
         Console.ReadKey(true); 
         Console.Clear(); 
        } 

        else if (result == true && delSelection >= tempList.Count || delSelection < 0) 
        { 
         Console.WriteLine("You've input a number that's outside the list. Input a digit between 0 and " + (tempList.Count - 1) + ".\n"); 

        } 

        else if (result == false) 
        { 
         Console.WriteLine("\nYou didn't input a digit, try again!"); 
         Console.ReadKey(true); 
        } 
       } while (result == false); 
+0

何が問題になりますか? –

答えて

0

私はあなたの範囲外の例外がここで起こることを想定しています:

tempInputDel = Convert.ToString(tempList[delSelection]);

delSelectiontempListの範囲内にある場合は、確認する前にこれを行うことはできません。最初にifブロック内に移動してください。

一般的なアドバイス:

あなたは(例えば)例外がスローされ、正確なソースコードの位置を見つけるために、文のステップ・バイ・ステップへのステートメントから行くためにデバッガを使用することができます。ユーザーが入力し、削除した項目を決定するための指標としてそれを使用しているどのような数本

tempInputDelInt = (int)delSelection; 

1

変更この

tempInputDelInt = (int)(tempList[delSelection]); 

。だから、tempListから得られる価値ではありません。

+0

ありがとう!問題は解決しました:) – Max

+0

@MaxModestoWallinあなたは彼の答えを受け入れることによって彼に感謝すべきです。 – RhinoDevel

関連する問題