名字生成器不工作 [英] First Name Generator not working
问题描述
我试图制作一个随机名称生成器,这是我想出的代码:
I was attempting to make a random name generator and this is the code I came up with:
private void GenerateName()
{
if (GeneratedName == "")
{
GenerateConsonant();
consonantCurrent = consonant1;
GenerateVowel();
vowelCurrent = vowel1;
GenerateConsonant();
consonantCurrent = consonant2;
GenerateConsonant();
consonantCurrent = consonant3;
GenerateVowel();
vowelCurrent = vowel2;
}
GeneratedName = consonant1 + vowel1 + consonant2 + consonant3 + vowel2;
}
private void GenerateConsonant()
{
Random randomNumber = new Random();
GlobalVariables.random = randomNumber.Next(2, 22);
if (GlobalVariables.random == 1)
{
consonantCurrent = "b";
}
if (GlobalVariables.random == 2)
{
consonantCurrent = "c";
}
if (GlobalVariables.random == 3)
{
consonantCurrent = "d";
}
if (GlobalVariables.random == 4)
{
consonantCurrent = "f";
}
if (GlobalVariables.random == 5)
{
consonantCurrent = "g";
}
if (GlobalVariables.random == 6)
{
consonantCurrent = "h";
}
if (GlobalVariables.random == 7)
{
consonantCurrent = "j";
}
if (GlobalVariables.random == 8)
{
consonantCurrent = "k";
}
if (GlobalVariables.random == 9)
{
consonantCurrent = "l";
}
if (GlobalVariables.random == 10)
{
consonantCurrent = "m";
}
if (GlobalVariables.random == 11)
{
consonantCurrent = "n";
}
if (GlobalVariables.random == 12)
{
consonantCurrent = "p";
}
if (GlobalVariables.random == 13)
{
consonantCurrent = "q";
}
if (GlobalVariables.random == 14)
{
consonantCurrent = "r";
}
if (GlobalVariables.random == 15)
{
consonantCurrent = "s";
}
if (GlobalVariables.random == 16)
{
consonantCurrent = "t";
}
if (GlobalVariables.random == 17)
{
consonantCurrent = "v";
}
if (GlobalVariables.random == 18)
{
consonantCurrent = "w";
}
if (GlobalVariables.random == 19)
{
consonantCurrent = "x";
}
if (GlobalVariables.random == 20)
{
consonantCurrent = "y";
}
if (GlobalVariables.random == 21)
{
consonantCurrent = "z";
}
}
private void GenerateVowel()
{
Random randomNumber = new Random();
GlobalVariables.random = randomNumber.Next(2, 6);
if (GlobalVariables.random == 1)
{
vowelCurrent = "a";
}
if (GlobalVariables.random == 2)
{
vowelCurrent = "e";
}
if (GlobalVariables.random == 3)
{
vowelCurrent = "i";
}
if (GlobalVariables.random == 4)
{
vowelCurrent = "o";
}
if (GlobalVariables.random == 5)
{
vowelCurrent = "u";
}
}
}
}
运行后,我发现它给了我一个空结果......如果有明显错误,请告诉我,因为我是智障:l或者,如果它永远行不通,您可以向我展示您可以使用的方法,然后我会尝试将其调整到我的程序中.
After running it, I found it gave me a null result... If there is an obvious error, please tell me as I am retarded :l Or if it would never work you could show me a way you would do it and then I would attempt to adapt it to my program.
顺便说一句,我正在制作一个大战略游戏,它将使用它为角色生成随机名称
Btw, I am making a Grand Strategy game that would use this to generate random names for characters
我试图做的是每次调用 GenerateConsonant() 和 GenerateVowel() 时,它们都会将一个随机字符设置为 Current 值.然后在每个设置后,它会将自己设置为某个辅音或元音.在此之后,它们将被放在一起形成一个名字......
What I attempted to do was every time GenerateConsonant() and GenerateVowel() were called they would set a random character to the Current value. Then after each was set it would set itself to a certain consonant or vowel. After this they'd all be put together to form a first name...
推荐答案
Logarr 意味着,而不是有
Logarr means that instead of having
consonantCurrent = consonant1;
你应该有
consonant1 = consonantCurrent;
其他变量赋值也是如此.
And the same goes for your other variable assignments.
此外,我建议在您的随机函数中返回辅音或元音,而不是将其分配给 consonantCurrent
或 vowelCurrent
.它为您节省了一个变量并且不那么令人困惑.
In addition, I would recommend returning a consonant or vowel in your random functions rather than assigning it to consonantCurrent
or vowelCurrent
. It saves you a variable and is less confusing.
我不确定这些是否属于更大的类,但请确保在为它们赋值之前初始化 consonant1
等变量.
I'm not sure if these are part of a larger class, but make sure you are initializing your consonant1
etc variables before assigning values to them.
这篇关于名字生成器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!