问题
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";
}
}
}
}
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
Edit: 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...
回答1:
Logarr means that instead of having
consonantCurrent = consonant1;
You should have
consonant1 = consonantCurrent;
And the same goes for your other variable assignments.
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.
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.
回答2:
That's a lot of bugs in such a small amount of code:
- New instance of
Randomfor each random number - Incorrect range for random number generation
- Reversed assignment
- Not using
yas a vowel?
Never mind the use of side-effects (class fields) instead of just returning values from methods, and the use of a huge series of if statements (not even if/else if!) to map an integer to a character.
Seems to me that something like this would be a lot easier to write correctly, and will be easier to read later:
static readonly string[] _vowels = { "a", "e", "i", "o", "u" };
static readonly string[] _consonants =
Enumerable.Range((int)'b', (int)'z' - (int)'b' + 1)
.Select(c => ((char)c).ToString())
.Except(_vowels)
.ToArray();
static readonly Random _random = new Random();
private static string GenerateName()
{
return GenerateConsonant()
+ GenerateVowel()
+ GenerateConsonant()
+ GenerateConsonant()
+ GenerateVowel();
}
private static string GenerateVowel()
{
return _vowels[_random.Next(_vowels.Length)];
}
private static string GenerateConsonant()
{
return _consonants[_random.Next(_consonants.Length)];
}
I'll leave it up to you to decide whether to count 'y' as a vowel as well as a consonant, and if so how to implement that. :)
回答3:
all your code can fit into a simple method to generate a random name
at the class level assign the Random Class
Random rnd = new Random();
private string Generate_Name(int length)
{
string name = "";
string[] letters = new string[21] { "b", "c", "d",
"f", "g", "h", "j",
"k", "l", "m", "n",
"p", "q", "r", "s", "t",
"v", "w", "x", "y", "z"};
string[] vowels = new string[5] { "a", "e", "i", "o", "u" };
for (int i = 0; i < length; i++)
{
if (i == 2 || i == 4)
{
int index = rnd.Next(0, vowels.Length);
name += vowels[index];
}
else
{
int index = rnd.Next(0, letters.Length);
name += letters[index];
}
}
return name;
}
then you can change the code around to fit what you need!
来源:https://stackoverflow.com/questions/27286285/first-name-generator-not-working