0

I'm making a basic Math game. I want the user/player to only be able to input numbers as the answer. I could just write a different code for letters but I would prefer if they couldn't use letters at all.

Not sure if I use Console.ReadLine(); or another function but I am only learning c# so bear with me.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
deviousPriest
  • 13
  • 1
  • 5
  • I suggest you go this way: http://stackoverflow.com/a/29526801/2321042 If you need that in several places, you could put that inside a method just like Eric suggested in his answer. – Andrew Nov 20 '16 at 04:06

1 Answers1

3

You don't. You use TryParse on the result, and if it is not an integer, you print "that's not an integer", loop back around, and try again.

Try writing a method that takes as its input a prompt string, a try-again string, and returns an integer:

static int PromptForInteger(string prompt, string tryAgain) 
{ ... }

You fill in the rest. Now you have a method you can use any time you want to prompt for an integer.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067