How Should I Approach Game Development?

Started by
20 comments, last by Ph4tM4N 12 years, 9 months ago
You can get rid of the female option altogether and do the string compare more easily:

String.Compare(strGender, "male", true);

That ignores the case of the user input. The way you did it, it wouldn't work if they entered "MAle" or "femalE"


I haven't compiled this but if you do something like below in your gender check loop, its a lot cleaner.


if (String.Compare(strGender, "male", true) == 0)
{
Console.WriteLine("So you are a man?");
}
else
{
Console.WriteLine("So you are a woman?");
}

string strGenderYesNo = Console.ReadLine();
if (String.Compare(strGenderYesNo, "yes", true) == 0)
{
gender = true;
}


You'll have to add some error checking though, in case the user enters something weird. You can message me if you need more help.
Advertisement
here's what I would do:

strGender = strGender.toLower();
if (strGender == "male" || strGender == "female")
; // do stuff
else
Console.WriteLine("Can you not answer a simple question?");



Assuming whatever language this is (C#?) has a sane implementation of strings that includes toLower and toUpper functions. if not, write your own toLower function that takes in a string and returns that string with every character modified to be lowercase. It makes your life easier. What if they type "MaLe" or "feMALE" etc.

and follow that up with

string strGenderYesNo = Console.ReadLine();
if(strGenderYesNo.toLower().startsWith('y'))
; // do what you need to do if they say yes


Once again, the startswith function may not exist in your language, but it probably does, and is also trivial to implement yourself. This way allows people to triple their productivity by only caring about the first letter. It's also faster computationally or whatever, but who cares about that.

Vent radioactive gas? ;-)

This topic is closed to new replies.

Advertisement