How do I only allow number input into my C# Console Application?

后端 未结 6 2098
鱼传尺愫
鱼传尺愫 2020-11-28 12:54
Console.WriteLine(\"Enter the cost of the item\");                           
string input = Console.ReadLine();
double price = Convert.ToDouble(input);
6条回答
  •  情深已故
    2020-11-28 13:32

    This MSDN article explains how to read characters one at a time in a console window. Test each character as it is input with the Char.IsNumber() method, and reject those characters that fail the test.

提交回复
热议问题