【发布时间】:2019-03-18 06:47:23
【问题描述】:
我正在制作一个在控制台中运行的简单的刽子手游戏。我创建了一个从列表中选择随机单词的方法,以便稍后实现作为答案。我创建了这个方法来做到这一点:
public static string GetWord()
{
Random random = new Random();
string[] words = new string[5]{"a", "b", "c", "d", "e"};
return words[random.Next(5)];
}
我通过使用 for 循环将该方法循环 100 次来测试该方法:
static void Man(string[] args)
{
for(int i = 0; i <101; i++)
{
Console.WriteLine(GetWord());
}
}
我希望得到一组随机的字母作为输出。但是,当我运行程序时,情况并非如此。相反,我得到类似于:
d d d d d d d d d d d d d
d d d C C C C e e e e 电子
e d d d d d 一种 一个
一种 一种 一种 一种 一种 一种 一种 b b b b b b b b b b
b c c c c e e e e e e e e d d d d a a a a a a a a a e e b b b b b b b b d d d d c c c c c c e
是不是我做错了什么?如果是这样,我该怎么做才能解决这个问题?提前谢谢你
【问题讨论】:
-
欢迎来到 StackOverflow。您的问题不太符合 StackOverflow 期望的标准。您当前状态下的问题可能不会被接受。我强烈建议您按照this StackOverflow article 的准则编辑您的问题
-
尝试在函数
GetWord之外写Random random = new Random();然后你会得到你想要的输出。
标签: c# random console-application generator