Console.WriteLine("Enter the cost of the item"); string input = Console.ReadLine(); double price = Convert.ToDouble(input);
你好,我想要键盘按钮,A-Z,括号,问号等被禁用.我想要它,如果你输入,它不会显示在控制台.我只想显示1-9号.这是在C#控制台应用程序.谢谢您的帮助!
解决方法
尝试这个代码片段
string _val = ""; Console.Write("Enter your value: "); ConsoleKeyInfo key; do { key = Console.ReadKey(true); if (key.Key != ConsoleKey.Backspace) { double val = 0; bool _x = double.TryParse(key.KeyChar.ToString(),out val); if (_x) { _val += key.KeyChar; Console.Write(key.KeyChar); } } else { if (key.Key == ConsoleKey.Backspace && _val.Length > 0) { _val = _val.Substring(0,(_val.Length - 1)); Console.Write("\b \b"); } } } // Stops Receving Keys Once Enter is Pressed while (key.Key != ConsoleKey.Enter); Console.WriteLine(); Console.WriteLine("The Value You entered is : " + _val); Console.ReadKey();