2016-04-26 13 views
0

私はハッカーゲームを作ろうとしていますが、私はコマンドを入力していますが、この問題が発生しています。 。引数が範囲外の例外、何をするか

これはProgram.csの

if (enter == "del" + space + FIles.files1[0] && admin == true && connected == true && cdMain == true) 
      { 
       FIles.files1.RemoveAt(0); 

       Console.WriteLine(""); 
       FIles.files1.ForEach(Console.WriteLine); 
       Console.ForegroundColor = ConsoleColor.Cyan; 
       Console.WriteLine(successfull); 
       Console.ForegroundColor = ConsoleColor.Green; 
      } 
      if (enter == "del" + space + FIles.files1[1] && admin == true && connected == true && cdMain == true) 
      { 
       FIles.files1.RemoveAt(1); 

       Console.WriteLine(""); 
       FIles.files1.ForEach(Console.WriteLine); 
       Console.ForegroundColor = ConsoleColor.Cyan; 
       Console.WriteLine(successfull); 
       Console.ForegroundColor = ConsoleColor.Green; 
      } 
if (enter == "del" + space + FIles.files1[2] && admin == true && connected == true && cdMain == true) 
      { 
       FIles.files1.RemoveAt(2); 

       Console.WriteLine(""); 
       FIles.files1.ForEach(Console.WriteLine); 
       // FIles.files1.AddRange(1); 
       Console.ForegroundColor = ConsoleColor.Cyan; 
       Console.WriteLine(successfull); 
       Console.ForegroundColor = ConsoleColor.Green; 

      } 
      if (enter == "del" + space + FIles.files1[3] && admin == true && connected == true && cdMain == true) 
      { 
       FIles.files1.RemoveAt(3); 

       Console.WriteLine(""); 
       FIles.files1.ForEach(Console.WriteLine); 
       Console.ForegroundColor = ConsoleColor.Cyan; 
       Console.WriteLine(successfull); 
       Console.ForegroundColor = ConsoleColor.Green; 
      } 

の最初のコードであり、ここで一覧

class FIles 
{ 
    public static List<string> files1 = new List<string>(); 

    public static void Files1() 
    { 
     files1.Add("file1.exe"); 
     files1.Add("file2.exe"); 
     files1.Add("file3.exe"); 
     files1.Add("file4.exe"); 
     files1.Add("file5.exe"); 
    } 

} 

エラーを有する第二ですが、私はthirthを削除しようとすると、助けてくださいに到着します。

for(int i = 0; i < ExampleList.Count; i++) 
{ 
    if (enter == "del" + space + FIles.files1[i] && admin == true && connected == true && cdMain == true) 
    { 
      FIles.files1.RemoveAt(i); 

      Console.WriteLine(""); 
      FIles.files1.ForEach(Console.WriteLine); 
      Console.ForegroundColor = ConsoleColor.Cyan; 
      Console.WriteLine(successfull); 
      Console.ForegroundColor = ConsoleColor.Green; 
    } 
} 

あなたのコードに問題があなたのリストのサイズは、あなたがRemoveAtを呼び出すたびに変化していることである。これについては何

+0

リストを初期化するためにFIles.Files1()を呼び出しますか?私はあなたの名前を追加することができますかかなり混乱...私はあなたが最初に静的なコンストラクタを持っていたと思った。 – Harry

+0

はい、Files.Files1()を呼び出してリストを初期化します。私はdel file1.exeを入力すると、それもうまく動作しますが、del file3.exeが必要な場合は、範囲外のエラー配列を返します。 –

+0

forループを使ってファイルのすべての要素を単純に反復してみてください。時間とそれがクラッシュする時点で参照してください。さらに、 'FIles.Files1()'にブレークポイントを設定して実際に呼び出されることを確認してください。 – Harry

答えて

0

。 2回の呼び出し後、リストのサイズは3で、FIles.files1[3]は範囲外です。

+1

ああ、あなたは最高のfkinです、今、私はもっと多くのことを学ばなければならないことを知っていることを知っている、私はあなたが大好きです、私はあなたを愛して、私は愚かです:( –

+0

ありがとう、私はこれを与える私の心に情報、非常に有用な大きな感謝:)、今私はこれで間違っていた理解しています:) –

関連する問題