【发布时间】:2018-03-28 07:16:31
【问题描述】:
我需要一些帮助来完成这个简单的名称“生成器”
我们刚开始在高中编程,所以我想要一个更简单的解决方案。这就是我现在所拥有的,但文本框中的输出是System.String[]
private void Btn_gen_Click(object sender, EventArgs e)
{
textbox.Text = textbox.Text + name((int) count.Value) + Environment.NewLine;
}
private string name(int count)
{
string heslo = "";
string[] names = new string[] {"Abby", "Uther", "Thomas", "Michelle", "Abraham", "Bendy"};
string[] surnames = {"The Mighty", "The Clumsy", "The Strong", "The Lightbringer", "The Pyromancer", "The Necromancer"};
Random gnč = new Random();
for (int i = 1; i <= count; i++)
{
int nč = gnč.Next(0, names.Length);
heslo = heslo + names;
}
return heslo;
}
【问题讨论】:
-
将循环中的最后一条语句替换为 --> heslo = heslo + names[nč]; ,如果您想给老师留下深刻印象,请将“heslo”字符串更改为 StringBuilder 并使用附加。当“计数”超过 3-4 时,性能更好
-
有什么问题?如果 Stackoverflow 有错误并且您不知道如何修复它,则此问题适用于 Stackoverflow。如果您的代码有效,但您只是想要评论,请尝试在 CodeReview StackExchange 上发布。