Reputation: 39
while (asteriskWord != hangCharacters && score < 5)
{
while ( letter != ) //This is where I want to test for A-Z a-z
{
Console.WriteLine("Enter a letter please");
letter = Convert.ToChar(Console.ReadLine());
}
}
After the first while-loop
I want to be able to place the input inside of a while loop that breaks if the value is = letters A-Z or a-z.
This is so that if the user enters the wrong information, the loop will continue
to ask for the information.
Thank you for your time and consideration. Also I tried doing letter != 'A' && letter != 'a'...... and so on, but it didn't work for some reason.
Upvotes: 0
Views: 1416
Reputation: 2017
Another option is to use a regular expression, like this:
while (!Regex.IsMatch(letter, "[a-zA-Z]"))
{
Console.WriteLine("Enter a letter please!");
letter = Console.ReadLine();
}
Upvotes: 3
Reputation: 1501163
Well the simplest way would be to just use comparisons. If you definitely want to do it in a single expression, Glorfindel's answer is appropriate. However, all those brackets and the negation makes me nervous of the readability. I'd break it into a separate method:
while (!IsValidLetter(letter))
{
...
}
...
private static bool IsValidLetter(char letter)
{
return (letter >= 'a' && letter <= 'z') ||
(letter >= 'A' && letter <= 'Z');
}
Whichever you find more readable - the latter is easier to modify later, of course, if you end up with more complicated requirements.
Upvotes: 8
Reputation: 22651
Characters can be compared to each other, just like numbers:
while (!((letter >= 'A' && letter <= 'Z') || (letter >= 'a' && letter <= 'z'))) {
Console.WriteLine("Enter a letter please");
letter = Convert.ToChar(Console.ReadLine());
}
Upvotes: 13