【问题标题】:Can Console.Clear be used to only clear a line instead of whole console?Console.Clear 可以用来只清除一行而不是整个控制台吗?
【发布时间】:2012-01-20 19:26:47
【问题描述】:

在为学校编写问答程序时,我突然想到我可以使用Console.Clear() 来清除屏幕上的所有内容。我想知道我是否可以使用Console.Readline(valueOne),然后只输出答案而不输出问题。如果我只问一个问题,Console.Clear 就可以了。

我有几个关于值而不是引用的问题,如果可能,请删除。我想省略问题,只显示几个答案。我想如果我存储答案,我可以使用Console.Clear() 然后只使用Console.WriteLine() 和三个变量。我可以这样做:

Console.WriteLine("Value 1 is: {0:c}" + "Value 2 is: {1:c}" + "Value 3 is: {2:c}, valueOne, valueTwo, valueThree).

使用引用更容易解决问题,因为值是存储和检索的。如果我只是简单地使用方法通过值传递并输出值,main() 将没有对这些值的引用以再次清除和输出。这就是为什么我想知道我是否可以只问一个问题,然后删除该行并只输出一个(或多个)答案。

我只是想了解可能性,而不是尝试设置程序。我想知道在没有额外输出问题的情况下从参考和按值输出值的能力。

【问题讨论】:

  • 顺便说一句,我只是 c# 的 3 周学生。
  • 你想做什么?我很难理解您对“答案”和“问题”的使用 - 它们是什么以及您正在构建什么?你的问题是什么?

标签: c#


【解决方案1】:

说明

您可以使用Console.SetCursorPosition 函数转到特定的行号。然后就可以用这个函数来清线了:

public static void ClearCurrentConsoleLine()
{
    int currentLineCursor = Console.CursorTop;
    Console.SetCursorPosition(0, Console.CursorTop);
    Console.Write(new string(' ', Console.WindowWidth)); 
    Console.SetCursorPosition(0, currentLineCursor);
}

样本

Console.WriteLine("Test");
Console.SetCursorPosition(0, Console.CursorTop - 1);
ClearCurrentConsoleLine();

更多信息

【讨论】:

  • 这段代码应该使用Console.BufferWidth而不是Console.WindowWidth,因为可能会有字符写在可见窗口区域之外。
  • 为什么使用(Console.CursorTop - 1) 不会导致超出范围错误?我正在查看SetCursorPosition 的C# 源代码,看起来它使用'int' 会使数字变为-1。我看到它没有使用无符号整数,那为什么不抛出错误呢?
  • @TheFluffyRobot 因为Console.CursorTop 返回当前行,所以Console.CursorTop - 1 返回上一行。由于前面有Console.WriteLine("Test");,所以当前行永远不会是0。
【解决方案2】:

一个更简单且更好的解决方案是:

Console.Write("\r" + new string(' ', Console.WindowWidth) + "\r");

它使用carriage return 转到行首,然后打印与控制台宽度一样多的空格并再次返回到行首,因此您可以在之后打印自己的测试。

【讨论】:

  • 对我来说它只适用于 Console.WindowWidth-1:Console.Write("\r" + new string(' ', Console.WindowWidth - 1) + "\r");
【解决方案3】:

"ClearCurrentConsoleLine"、"ClearLine" 和上面的其余函数应该使用 Console.BufferWidth 而不是 Console.WindowWidth(当您尝试使窗口变小时,您会明白为什么)。控制台的窗口大小当前取决于它的缓冲区,不能比它宽。 示例(感谢 Dan Cornilescu):

public static void ClearLastLine()
{
    Console.SetCursorPosition(0, Console.CursorTop - 1);
    Console.Write(new string(' ', Console.BufferWidth));
    Console.SetCursorPosition(0, Console.CursorTop - 1);
}

【讨论】:

  • 这通常应该是对相应帖子的评论,因为它本身并不能回答问题。
  • 当我没有足够的声誉时,我无法回复他们的帖子,我无法联系 cmets 的作者,因为找不到他们的电子邮件,并且没有必要发送相同的垃圾邮件(非原创)我的答案中的解决方案。
  • if (Console.CursorTop > 0) Console.SetCursorPosition(0, Console.CursorTop - 1);
【解决方案4】:

这对我有用:

static void ClearLine(){
    Console.SetCursorPosition(0, Console.CursorTop);
    Console.Write(new string(' ', Console.WindowWidth)); 
    Console.SetCursorPosition(0, Console.CursorTop - 1);
}

【讨论】:

    【解决方案5】:

    我的首选方法是使用 PadRight。这不是先清除该行,而是在显示新文本后清除该行的其余部分,节省一步:

    Console.CursorTop = 0;
    Console.CursorLeft = 0;
    Console.Write("Whatever...".PadRight(Console.BufferWidth));
    

    【讨论】:

    • (BufferWidth -1) 不会溢出行。
    【解决方案6】:

    我们可以简单的写如下方法

    public static void ClearLine()
    {
        Console.SetCursorPosition(0, Console.CursorTop - 1);
        Console.Write(new string(' ', Console.WindowWidth));
        Console.SetCursorPosition(0, Console.CursorTop - 1);
    }
    

    然后像这样在需要时调用它

    Console.WriteLine("Test");
    ClearLine();
    

    对我来说很好用。

    【讨论】:

      【解决方案7】:

      要从当前位置清除到当前行的末尾,请执行以下操作:

          public static void ClearToEndOfCurrentLine()
          {
              int currentLeft = Console.CursorLeft;
              int currentTop = Console.CursorTop;
              Console.Write(new String(' ', Console.WindowWidth - currentLeft));
              Console.SetCursorPosition(currentLeft, currentTop);
          }
      

      【讨论】:

        【解决方案8】:
        public static void ClearLine(int lines = 1)
        {
            for (int i = 1; i <= lines; i++)
            {
                Console.SetCursorPosition(0, Console.CursorTop - 1);
                Console.Write(new string(' ', Console.WindowWidth));
                Console.SetCursorPosition(0, Console.CursorTop - 1);
            }
        }
        

        【讨论】:

        • 请解释你的答案,以便对未来的读者有用。
        【解决方案9】:

        我想我找到了为什么这个问题有几个不同的答案。当窗口已调整大小以使其具有水平滚动条(因为缓冲区大于窗口)时,Console.CursorTop 似乎返回了错误的行。无论窗口大小或光标位置如何,以下代码都适用于我。

        public static void ClearLine()
        {
            Console.SetCursorPosition(0, Console.CursorTop);
            Console.Write(new string(' ', Console.WindowWidth));
            Console.SetCursorPosition(0, Console.CursorTop - (Console.WindowWidth >= Console.BufferWidth ? 1 : 0));
        }
        

        如果没有 (Console.WindowWidth >= Console.BufferWidth ? 1 : 0),代码可能会向上或向下移动光标,具体取决于您在此页面中使用的版本以及窗口的状态。

        【讨论】:

          【解决方案10】:

          正如我所料,这对我来说非常适合 -

              public static void ClearLine()
              {
                  Console.SetCursorPosition(0, Console.CursorTop);
                  Console.Write(new string(' ', Console.WindowWidth));
                  Console.SetCursorPosition(0, Console.CursorTop);
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-01-01
            • 2010-09-27
            • 1970-01-01
            • 2011-09-08
            • 1970-01-01
            • 2020-05-06
            • 2015-04-23
            相关资源
            最近更新 更多