I don't quite know where to post this but C# is not my first language and I am hoping to get someone to nod their head or correct me :)
This works but seems counter intuitive - mostly because I would like to make a list and then say list contains oneCharacter but, for cases like this, it would be excessively wordy.
The only purpose of this sample code is to evaluate whether oneCharacter is in the string "YN" and to ask if there is a better (or more beautiful) way to do this. It is not any more complicated than that.
void MainGameLoop()
{
while(true)
{
Console.WriteLine("Randomizing new string ...");
var randomizedString = RandomizeString();
Console.WriteLine("Please input the value, only first letter of the input will be taken into account");
var userInput = Console.ReadLine();
var userCharacter = userInput[0].ToString();
// Here is the code I'm hoping to sort out. Is this a reasonable approach within the If condition?
if(!randomizedString.Contains(userCharacter))
{
Console.WriteLine("Sorry, wrong guess.");
}
// end code I'm hoping to sort out
else
{
Console.WriteLine("You guessed it!");
}
}
}
Contains
is okay. It says exactly what you intend to check. In case you are curious, underneath it actually calls (inyour case)"YN".IndexOf(oneCharacter, StringComparison.Ordinal) >=0
which is a search within an array. \$\endgroup\$ – MaLiN2223 Sep 14 '18 at 20:15oneCharacter
guaranteed to contain exactly one character? (What if it is the string"YN"
, or an empty string?) \$\endgroup\$ – 200_success Sep 14 '18 at 20:15