C# 短语生成器不断重复相同的短语
我对C#很陌生,我觉得做这个会是一个有趣的小挑战。在搜索了很多和我有同样问题的帖子之后,没有人能帮到我。每次我调试它时,短语是不同的,但是在调试时,它会重复相同的短语,而不是每次都不同C# 短语生成器不断重复相同的短语,c#,C#,我对C#很陌生,我觉得做这个会是一个有趣的小挑战。在搜索了很多和我有同样问题的帖子之后,没有人能帮到我。每次我调试它时,短语是不同的,但是在调试时,它会重复相同的短语,而不是每次都不同 using System; public class Program { static String[] nouns = new String[3] { "He", "She", "It" }; static String[] adjectives = new String[5] { "lou
using System;
public class Program
{
static String[] nouns = new String[3] { "He", "She", "It" };
static String[] adjectives = new String[5] { "loudly", "quickly", "poorly", "greatly", "wisely" };
static String[] verbs = new String[5] { "climbed", "danced", "cried", "flew", "died" };
static Random rnd = new Random();
static int noun = rnd.Next(0, nouns.Length);
static int adjective = rnd.Next(0, adjectives.Length);
static int verb = rnd.Next(0, verbs.Length);
static void Main()
{
for (int rep = 0; rep < 5; rep++ )
{
Console.WriteLine("{0} {1} {2}", nouns[noun], adjectives[adjective], verbs[verb]);
}
}
}
使用系统;
公共课程
{
静态字符串[]名词=新字符串[3]{“他”、“她”、“它”};
静态字符串[]形容词=新字符串[5]{“大声”、“迅速”、“糟糕”、“极大”、“明智”};
静态字符串[]动词=新字符串[5]{“爬”、“跳”、“哭”、“飞”、“死”};
静态随机rnd=新随机();
静态int noun=rnd.Next(0,nomes.Length);
静态int形容词=rnd.Next(0,形容词.Length);
静态int-verb=rnd.Next(0,verbs.Length);
静态void Main()
{
对于(int rep=0;rep<5;rep++)
{
Console.WriteLine({0}{1}{2}),名词[名词],形容词[形容词],动词[动词];
}
}
}
当程序首次加载时,静态变量只初始化一次
您需要在每次打印新短语时(重新)生成名词
,形容词
,和动词
,因此您应该在循环中移动它们,如下所示:
static void Main()
{
for (int rep = 0; rep < 5; rep++ )
{
int noun = rnd.Next(0, nouns.Length);
int adjective = rnd.Next(0, adjectives.Length);
int verb = rnd.Next(0, verbs.Length);
Console.WriteLine("{0} {1} {2}", nouns[noun], adjectives[adjective], verbs[verb]);
}
}
static void Main()
{
对于(int rep=0;rep<5;rep++)
{
int noun=rnd.Next(0,nomes.Length);
int形容词=rnd.Next(0,形容词长度);
int verb=rnd.Next(0,verbs.Length);
Console.WriteLine({0}{1}{2}),名词[名词],形容词[形容词],动词[动词];
}
}
这样,每次运行循环时都会生成新的随机值