【问题标题】:How do I tell my console app not to continue until i enter a number?如何告诉我的控制台应用程序在输入数字之前不要继续?
【发布时间】:2017-08-26 20:58:10
【问题描述】:
static int beverageSelection()
{
    Console.WriteLine();

    int brand;

    string _val = "";
    Console.Write("Enter number: ");
    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");
            }
        }
    }
    while (key.Key != ConsoleKey.Enter);

    brand = Convert.ToInt32(Console.ReadLine());

    return brand;
}

上面的方法让我很头疼。我不知道如何告诉控制台应用程序它不应该让我输入任何字符甚至输入按钮,直到我在控制台中输入了一个数字。然后只有那时我才能按回车键。 无论如何,这个程序是我为了好玩而创建的自动售货机,为了清楚起见,我并不完全理解这段代码中的 do while 循环。

【问题讨论】:

  • 变量名是德文的,很快就改成英文了
  • 不要试图阻止错误输入...只需检查输入是否有效(如果无效,告诉用户并等待另一个输入)

标签: c# .net console


【解决方案1】:

使用Console.ReadLine 代替Console.ReadKey:

int brand = 0;
while (true)
{
    string val = Console.ReadLine();
    if (int.TryParse(val, out brand)) break;
}

【讨论】:

    【解决方案2】:

    改进了您的原始代码以仅显示和接受数字

        static void Main(string[] args)
        {
            Console.WriteLine();
    
            int brand;
    
            string _val = "";
            Console.Write("Enter number: ");
            while(true)
            {
                var key = Console.ReadKey(true);
                if (key.Key == ConsoleKey.Enter && int.TryParse(_val, out brand))
                {
                    Console.WriteLine();
                    break;
                }
                if (key.Key != ConsoleKey.Backspace)
                {
                    int val;
                    if (int.TryParse(key.KeyChar.ToString(), out val))
                    {
                        _val += key.KeyChar;
                        Console.Write(key.KeyChar);
                    }
                }
                else
                {
                    if (_val.Length > 0)
                    {
                        _val = _val.Substring(0, _val.Length - 1);
                        Console.Write("\b \b");
                    }
                }
            }
    
            Console.WriteLine("Brand: {0}", brand);
            Console.ReadKey();
        }
    

    【讨论】:

      【解决方案3】:

      为了完整起见,如果有人希望控制台在选择数值之前阻止非数字字符,您可以简单地使用这个无误的代码来完成您的任务:

      int inputBoundary = Console.CursorLeft;
      string consoleInput = String.Empty;
      ConsoleKeyInfo inputKey;
      while((inputKey = Console.ReadKey(true)).Key != ConsoleKey.Enter || consoleInput == String.Empty)
      {
          if (inputKey.Key == ConsoleKey.Backspace && Console.CursorLeft != inputBoundary)
          {
              Console.Write("\b \b");
              consoleInput = consoleInput.Remove(consoleInput.Length - 1);
              continue;
          }
          if (Char.IsNumber(inputKey.KeyChar))
          {
              Console.Write(inputKey.KeyChar);
              consoleInput += inputKey.KeyChar;
          }
      }
      int selection = Int32.Parse(consoleInput);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-12-03
        • 1970-01-01
        • 1970-01-01
        • 2012-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多