Use
int.TryParse[
^] to attempt to convert the
string
to an
int
. You'll also need to account for cases where the user doesn't type a valid number.
string userText = Console.ReadLine();
int userValue;
while (!int.TryParse(userText, out userValue))
{
Console.WriteLine("Sorry, I didn't understand that.");
Console.WriteLine("Pick a number: 1, 2 or 3.");
userText = Console.ReadLine();
}
string message = (userValue == number) ? "You won the Lottery!" : "Sorry, you didn't get the right number. Better luck next time!";
Console.WriteLine("{0} The number you typed was {1}.", message, userValue);
Console.ReadLine();