可以使用Console.Clear仅清除一行而不是整个控制台吗?


77

在为学校编写问题/答案程序时,我想到可以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()则不会引用这些值来清除并再次输出。这就是为什么我想问我是否可以问一个问题,然后删除一行并仅输出答案(或一个答案)的原因。

我只是试图了解可能性,而不是尝试设置程序。我想知道从参考值和按值输出值的能力,而无需额外的输出问题。


1
顺便说一下,我只是C#的3周学生。
坏男孩

1
你想做什么?我很难理解您对“答案”和“问题”的使用-它们是什么,您要构建什么?那你的问题是什么?
Michael Banzon 2012年

Answers:


141

描述

您可以使用该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();

更多信息


我现在对.NET编程已有8-9年的时间了...所以不要惊慌。阅读书籍,找到合适的人来关注和观看网络广播。
dknaack 2012年

1
@badboy-Google / Bing + MSDN是您的在线朋友-如果您不确定方法-进行搜索,然后首先尝试MSDN回答以获取官方说明。但是,了解API只是编程的一部分...这就是为什么他们修课程,学位并且需要大量练习...欢迎来到我希望您找到一条有趣的道路的原因。
肖恩·王尔德

7
此代码可能应该使用Console.BufferWidth而不是Console.WindowWidth,因为有可能在可见窗口区域之外写入字符。
NathanAldenSr '16

为什么服用(Console.CursorTop - 1)不会引起超出范围的错误?我正在看SetCursorPosition的C#源代码,看起来它使用的是'int',它将使数字为-1。我看到它不使用无符号整数,所以为什么不抛出错误?
蓬松的机器人

@TheFluffyRobot因为Console.CursorTop返回当前行,所以Console.CursorTop - 1返回前一行。由于存在Console.WriteLine("Test");之前,当前行不为0
磁控

45

一个更简单,更完美的解决方案是:

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

它使用回车键返回到行的开头,然后打印与控制台宽度相同的空格,然后再次返回到行的开头,因此您可以在以后打印自己的测试。


27
对我来说,它仅适用于Console.WindowWidth-1: Console.Write("\r" + new string(' ', Console.WindowWidth - 1) + "\r");
Dorin 2013年

12

“ 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);
}

这通常应该是对各个帖子的评论,因为它本身无法回答问题。
Dan Cornilescu

7
当我没有足够的声誉时,我无法回答他们的帖子,由于找不到他们的电子邮件,并且无法在我的答案中散布相同的(不是原始的)解决方案,因此无法联系评论的作者。
User0123456789 '16

9

这为我工作:

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)不溢出线。
riezebosch

2

我们可以简单地编写以下方法

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();

这对我来说可以。


1

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

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

1
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);
    }
}

3
请解释您的答案,以便对将来的读者有用。
Prudhvi 2015年

0

我想我找到了为什么这个问题有几个不同的答案。调整窗口大小后,使其具有水平滚动条(因为缓冲区大于窗口),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),则代码可能会向上或向下移动光标,具体取决于您在此页面上使用的版本以及窗口的状态。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.