如何在输入提示期间禁止控制台中的所有非数字输入?

Pre*_*ion 6 c# validation console input

在这里谈论控制台

这个想法是,如果用户在控制台的输入提示中按下除数字之外的任何键(字母键和numpad之上的键),则不会输入任何内容.它好像控制台将忽略任何非数字键按下.

如何以正确的方式做到这一点?

Ski*_*izz 9

尝试使用ReadKey方法:

while (processing input)
{
  ConsoleKeyInfo input_info = Console.ReadKey (true); // true stops echoing
  char input = input_info.KeyChar;

  if (char.IsDigit (input))
  {
     Console.Write (input);
     // and any processing you may want to do with the input
  }
}
Run Code Online (Sandbox Code Playgroud)