dimanche 27 décembre 2020

How can I use if-else statement in c#?

I would like to create a console application. In this application user have to type number between 1 and 9. I am using asscii code for reach the numbers. But my IF statement not works correctly. In the second statement, it always returns true. Where am I doing wrong?

// Get the ascii number of the key the user has entered.
int userInputAscii = Console.Read();

// Convert userInput to integer
string userInput = Char.ConvertFromUtf32(userInputAscii);

if (userInputAscii > 48 && userInputAscii < 58) 
{
  Console.WriteLine("Correct!");
}
else if (userInputAscii > 57 || userInputAscii < 48)
{
 Console.WriteLine("Wrong characters! Try again.");
}

Aucun commentaire:

Enregistrer un commentaire