我对编程非常陌生,仍然缺乏很多信息,但想深入研究它并从经验/建设性批评中学习。
我当前的问题是让控制台从字符串数组中打印随机字符串。我已经写了一些代码,但不确定如何初始化它或者它是否是正确的方法。
下面是字符串数组和其他几行代码,我认为可能可以帮助我到达需要的位置,但我知道我错过了很多。
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/