C# 从文本文档中选择随机字符串

C# 从文本文档中选择随机字符串,c#,text,console,document,C#,Text,Console,Document,我制作了一个程序,要求用户在文本文档中输入20个名称。如果文件不存在,创建一个新文件,如果存在,则显示内容 我想做的是用一个随机化器随机选择一个名字,我已经在这里做了,但我不能让它工作。我希望它读取文本文件,并从中随机选择一个名称。 我没有犯错误,也不确定我做错了什么 注意:我还做了一件事,就是在数组中输入类名[0]的名字,以增加被选中的机会 static void increaseChances() { int rand = r.Next(3); //0 = 100%, 1 = 50%

我制作了一个程序,要求用户在文本文档中输入20个名称。如果文件不存在,创建一个新文件,如果存在,则显示内容

我想做的是用一个随机化器随机选择一个名字,我已经在这里做了,但我不能让它工作。我希望它读取文本文件,并从中随机选择一个名称。 我没有犯错误,也不确定我做错了什么

注意:我还做了一件事,就是在数组中输入类名[0]的名字,以增加被选中的机会

static void increaseChances()
{
    int rand = r.Next(3); //0 = 100%, 1 = 50%, 2 = 33.33% chance, 3 = 25% chance, This number determines the percentage of the first name entered to be picked

    if (rand == 0)
    {
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("\nThe winner of the randomiser is: {0} Congratulations! ", classNames[0]);
    }
    else
    {
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("\nThe winner of the randomiser is: {0} Congratulations! ", classNames[r.Next(classNames.Length)]);
        Console.ForegroundColor = ConsoleColor.White;
    }
}
以下是我得到的:

class Program
{
    static Random r = new Random();
    static string[] classNames = new string[20];

    static void increaseChances()
    {
        int rand = r.Next(3); //0 = 100%, 1 = 50%, 2 = 33.33% chance, 3 = 25% chance, This number determines the percentage of the first name entered to be picked

        if (rand == 0)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("\nThe winner of the randomiser is: {0} Congratulations! ", classNames[0]);
        }
        else
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("\nThe winner of the randomiser is: {0} Congratulations! ", classNames[r.Next(classNames.Length)]);
            Console.ForegroundColor = ConsoleColor.White;
        }
    }

    static void Main(string[] args)
    {
        Random RandString = new Random();

        string file = @"C:\names.txt";
        Console.ForegroundColor = ConsoleColor.White;

        if (File.Exists(file))
        {
            Console.WriteLine("Names in the text document are: \n");
            foreach (string displayFile in File.ReadAllLines(file))
            Console.WriteLine(displayFile);
            increaseChances();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("\nPress any key to close... ");
            Console.ReadKey();
        }
        else
        {
            for (int i = 0; i < 20; i++) 
            {
                Console.Write("Enter name number {0}: ", i + 1);
                classNames[i] = Console.ReadLine();
                File.Create(file).Close();
                File.WriteAllLines(file, classNames);
            }

                Console.WriteLine("Writing names to file...");
                increaseChances();
                Thread.Sleep(3000);
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("Completed! Exiting...");
                Thread.Sleep(1500);
        }
    }
}

如果文件已经存在,则不填充classNames数组。因此,您的IncreaseOpportunities方法无法从中选择名称。

什么不起作用?你知道random并不是真正的random吗?你忘记的重要细节是:你没有得到任何错误,但是发生了你不想发生的事情?什么不起作用?我想让它读取文本文件并从中选择一个随机名称并显示它。你第一次使用r.Next是错误的。r、 Next1总是给0,r.Next2有50/50的机会为0或1等。您下次使用r.next是正确的,因为它永远不会返回类名。长度和导致索引异常,但它也有选择0的机会,这将改变您选择索引0的概率。@whiskybrah-现在它做什么?出了什么问题?是的,修复了我所做的:StreamReader sr=newstreamreaderfile;int i=0;而i