【问题标题】:C# Console Clear Input LineC# 控制台清除输入行
【发布时间】:2018-10-29 23:12:05
【问题描述】:

我有一个 C# 控制台程序,它通过显示输入提示来询问用户某个单词。然后使用 switch 语句处理输入。如果单词正确,则程序继续。但如果输入不匹配,程序会显示“错误:输入无效”,然后返回输入提示。棘手的部分是我希望程序清除用户在按下 Enter 之前输入的输入并再次提示用户,而不是在第一个提示下方再单独提示。

C# 中是否有某种库可以做类似的事情,还是我必须为它创建一个库?

【问题讨论】:

  • 查看您当前拥有的代码会很有帮助(与此问题相关)。
  • 专业提示,我们不使用自然语言编写代码是有原因的。因为自然语言是模棱两可的,不精确的。简而言之,我们需要在 100 次中看到 99 次而不是描述、经验证据和具体示例的代码。其他任何事情都让我们很难

标签: c# console prompt


【解决方案1】:

一种方法是使用Console.SetCursorPositionConsole.Write 的组合将光标设置为响应的开头,写入足够的空格以“擦除”响应,然后将光标设置回重新开始。

例如:

static string GetUserInput(string prompt, List<string> validResponses)
{
    Console.Write(prompt);

    // Capture the cursor position just after the prompt
    var inputCursorLeft = Console.CursorLeft;
    var inputCursorTop = Console.CursorTop;

    // Now get user input
    string input = Console.ReadLine();

    while (validResponses != null &&
           validResponses.Any() &&
           !validResponses.Contains(input, StringComparer.OrdinalIgnoreCase))
    {

        Console.ForegroundColor = ConsoleColor.Red;
        // PadRight ensures that this line extends the width
        // of the console window so it erases itself each time
        Console.Write($"Error! '{input}' is not a valid response".PadRight(Console.WindowWidth));
        Console.ResetColor();

        // Set cursor position to just after the promt again, write
        // a blank line, and reset the cursor one more time
        Console.SetCursorPosition(inputCursorLeft, inputCursorTop);
        Console.Write(new string(' ', input.Length));
        Console.SetCursorPosition(inputCursorLeft, inputCursorTop);

        input = Console.ReadLine();
    }

    // Erase the last error message (if there was one)
    Console.Write(new string(' ', Console.WindowWidth));

    return input;
}

在使用中可能如下所示:

static void Main(string[] args)
{
    var validResponses = new List<string> {"Yes", "No"};

    var userInput = GetUserInput("Do you like me? ", validResponses);

    if (userInput.Equals("Yes", StringComparison.OrdinalIgnoreCase))
    {
        Console.WriteLine("I like you too!");
    }
    else
    {
        Console.WriteLine("And all along I thought you had good taste.");
    }

    GetKeyFromUser("\nDone! Press any key to exit...");
}

这是该程序的示例运行。我必须包含几个屏幕截图,因为每次迭代都会清除响应(然后是错误消息):

【讨论】:

    【解决方案2】:

    试试这个……

    static void Main(string[] args)
    {
    
    CheckWord();
    Console.ReadKey();
    }
    
        private static void CheckWord()
        {
            while (true)
            {
                string errorMessage = "Error: invalid input ... enter a valid entry";
                string word = Console.ReadLine(); 
                if (word != "word")
                {
                    Console.SetCursorPosition(word.Length +1 , (Console.CursorTop) - 1);
                    Console.Write(errorMessage);
                    Console.ReadKey();
                    Console.SetCursorPosition(0, Console.CursorTop);
                    for (int i = 0; i <= word.Length + errorMessage.Length +1 ; i++)
                    {
                        Console.Write(" ");
                    }
                    Console.SetCursorPosition(0, Console.CursorTop);
                }
                else
                {
    
                    break;
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 2019-08-25
      相关资源
      最近更新 更多