c# - 想知道如何完成一个函数,让控制台从包含 20 个答案的数组中写出一个随机字符串?

标签 c#

我对编程非常陌生,仍然缺乏很多信息,但想深入研究它并从经验/建设性批评中学习。

我当前的问题是让控制台从字符串数组中打印随机字符串。我已经写了一些代码,但不确定如何初始化它或者它是否是正确的方法。

下面是字符串数组和其他几行代码,我认为可能可以帮助我到达需要的位置,但我知道我错过了很多。

public static void main()
        {
            string[] randOut = new string[20];
            randOut[0] = "It is certain.";
            randOut[1] = "It is decidedly so.";
            randOut[2] = "Without a doubt.";
            randOut[3] = "Yes- definitely.";
            randOut[4] = "You may rely on it.";
            randOut[5] = "As I see it, yes.";
            randOut[6] = "Most likely.";
            randOut[7] = "Outlook good.";
            randOut[8] = "Yes";
            randOut[9] = "Signs point to yes.";
            randOut[10] = "Reply hazy, try again.";
            randOut[11] = "Ask again later.";
            randOut[12] = "Better not tell you now.";
            randOut[13] = "Cannot predict now.";
            randOut[14] = "Concentrate and ask again.";
            randOut[15] = "Don't count on it.";
            randOut[16] = "My reply is no.";
            randOut[17] = "My sources say no.";
            randOut[18] = "Outlook not so good.";
            randOut[19] = "Very doubtful.";
            Random rnd = new Random();
            Console.WriteLine(randOut[rnd.Next(0,19)]);
        }

我希望这将允许控制台从列表中打印出随机字符串,但在写入介绍性字符串后,什么也没有发生。这段代码没有发现错误。

最佳答案

就在

之后
Console.WriteLine(randOut[rnd.Next(0,19)]);
Console.ReadLine();

查看控制台结果。在这种情况下,当主线程完成执行时,它将完成应用程序。

关于c# - 想知道如何完成一个函数,让控制台从包含 20 个答案的数组中写出一个随机字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56276968/

相关文章:

c# - 更改样式控件中的部分控件模板?

c# - 获取 QuickFix/n 的 session 属性(用户名和密码)

c# - 尝试部署 dapac : The path specified by File Ggroup FG. mdf 文件不在有效目录中时,SQL Linux docker 容器抛出错误

c# - 完整的 .NET OpenCL 实现

c# - 如何从 C# 中的 linq 数据中删除空格

c# - Roslyn,如何获得所有类(class)

c# - 为什么匿名方法不能赋值给var?

c# - 使用 FFMPEG/FFMediaElement (FFME) 在 WPF 应用程序中以低延迟播放 RTSP

c# - Newton Soft Json JsonSerializerSettings for object with Property as byte array

c# - Xamarin 自动导入快捷方式