-4

very recently I started to learn programming, started mostly with theory before getting my hands on the computer, question is, I need to build a program through the console on visual studios where the problem is, "make a program where a user needs to pick a number from 1 to 9, after he picks the number you have to make the program show " The number you picked is "X" "

I am trully a beginner, I don't know how to validate just numbers 1 to 9.

1 Answers1

1

Here is an example that uses int.TryParse to check if the input you gave is really a number (integer). The while loop iterates until the input you gave meets your criteria.

Console.WriteLine("Pick a number from 1 to 9");
int num;
while(!int.TryParse(Console.ReadLine(), out num) || num < 1 || num > 9)
{
     Console.WriteLine("Your entry was wrong!");
     Console.WriteLine("Pick a number from 1 to 9");
}
Console.WriteLine($"The number you picked is {num}");
Console.ReadKey();
Slaven Tojić
  • 2,945
  • 2
  • 14
  • 33