2016-04-26 73 views
0

我试图让黑客游戏,我有自己的CMD,我在那里打字commnads,但我得到这个问题,我不知道我在做什么坏。参数超出范围异常,怎么办

这是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抵达,请大家帮忙。

+0

你打电话给FIles.Files1()初始化列表吗?我可以添加你的命名是非常困惑的......我以为你有一个静态构造函数。 – Harry

+0

是的,我打电话Files.Files1()初始化列表。当我键入del file1.exe时,它工作得很好,但是如果我想要del file3.exe它会说错误数组超出范围 –

+0

尝试使用for循环遍历文件的所有元素,每次打印当前索引时间,看看它在哪一点崩溃。更好的是,在'FIles.Files1()'中放置一个断点以确保它真的被调用。 – Harry

回答

0

这个怎么样:

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时间的变化。两次通话后,您的列表的大小为3,FIles.files1[3]超出范围。

+1

哦,男人你是最好的,谢谢你的男人,现在我知道我必须学习更多,越来越多,非常感谢你,我爱你,我很愚蠢:( –

+0

哦,谢谢你,我给这个信息在我的脑海,非常有帮助大谢谢:),现在我明白这是什么错:) –