C#Sharp列表(超出范围异常错误)

时间:2017-11-05 21:46:20

标签: c#

我想对我的计划提供一些帮助。它应该随机化我班级的座位顺序。但是,我不断得到一个“超出范围的例外”。虽然所有索引都以0开头,但我试过但是无法解决它。我怎么摆脱这个。如果可能,是否可以不重复列表中的元素?我为我不准确的问题道歉,但这是我的代码......

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        List<string> list = new List<string>();
        list.AddRange(new String[]
        {
            "Daria", "Denisa", "Erica",
            "Merlin", "Nicoletta", "Mia",
            "Lilian", "Karel", "Luveesh",
            "Milan", "Oliver","Tea",
            "Carlos", "Raneem", "Marsha",
            "Uros", "Oguzhan"
        });

        Random random = new Random();
        Console.WriteLine("Type 'x' then hit 'Enter' to exit");

        string userInput = Console.ReadLine();

        if (userInput == "x")
        {
            foreach (var item in list)
            {
                Console.WriteLine("");
                Console.WriteLine("Table 1: " + "" + list[random.Next(1, list.Count)] + "," + "" + list[random.Next(2, list.Count)]);
                Console.WriteLine("Table 2: " + "" + list[random.Next(3, list.Count)] + "," + "" + list[random.Next(4, list.Count)]);
                Console.WriteLine("Table 3: " + "" + list[random.Next(5, list.Count)] + "," + "" + list[random.Next(6, list.Count)]);
                Console.WriteLine("Table 4: " + "" + list[random.Next(7, list.Count)] + "," + "" + list[random.Next(8, list.Count)]);
                Console.WriteLine("Table 5: " + "" + list[random.Next(9, list.Count)] + "," + "" + list[random.Next(10, list.Count)]);
                Console.WriteLine("Table 6: " + "" + list[random.Next(11, list.Count)] + "," + "" + list[random.Next(12, list.Count)]);
                Console.WriteLine("Table 7: " + "" + list[random.Next(13, list.Count)] + "," + "" + list[random.Next(14, list.Count)]);
                Console.WriteLine("Table 8: " + "" + list[random.Next(15, list.Count)] + "," + "" + list[random.Next(16, list.Count)]);
                Console.WriteLine("Table 9: " + "" + list[random.Next(17, list.Count)]);
                Console.ReadLine(); 
            }
        }
    }
}

}

2 个答案:

答案 0 :(得分:2)

C#为0索引,从0开始,而不是从1开始。

答案 1 :(得分:2)

您的list包含17个元素,其中第一个元素通过list[0]访问  和list[16]的最后一个。在你的foreach循环的最后一行..

//                                   <------------ here ------------>
Console.WriteLine("Table 9: " + "" + list[random.Next(17, list.Count)];

..您使用random.Next(min, max)生成随机数。 MSDN指出min是随机数的包含下界,max是返回的随机数的唯一上界。所以问题是,你尝试创建一个17(包括)为min的随机数,列表计数为17(exlusive),为max。因此生成的随机数为17.由于C#列表为零,访问list[17]会引发上述异常。

修改foreach循环中的代码,如.. < / p>

foreach (var item in list)
{
    Console.WriteLine("");
    Console.WriteLine("Table 1: " + "" + list[random.Next(0, list.Count)] + "," + "" + list[random.Next(1, list.Count)]);
    Console.WriteLine("Table 2: " + "" + list[random.Next(2, list.Count)] + "," + "" + list[random.Next(3, list.Count)]);
    Console.WriteLine("Table 3: " + "" + list[random.Next(4, list.Count)] + "," + "" + list[random.Next(5, list.Count)]);
    Console.WriteLine("Table 4: " + "" + list[random.Next(6, list.Count)] + "," + "" + list[random.Next(7, list.Count)]);
    Console.WriteLine("Table 5: " + "" + list[random.Next(8, list.Count)] + "," + "" + list[random.Next(9, list.Count)]);
    Console.WriteLine("Table 6: " + "" + list[random.Next(10, list.Count)] + "," + "" + list[random.Next(11, list.Count)]);
    Console.WriteLine("Table 7: " + "" + list[random.Next(12, list.Count)] + "," + "" + list[random.Next(13, list.Count)]);
    Console.WriteLine("Table 8: " + "" + list[random.Next(14, list.Count)] + "," + "" + list[random.Next(16, list.Count)]);
    Console.WriteLine("Table 9: " + "" + list[random.Next(15, list.Count)]);
    Console.ReadLine();
}

..应该这样做。