在 .net 控制台应用程序中显示百分比

发布于 2024-08-04 01:51:17 字数 110 浏览 2 评论 0原文

我有一个控制台应用程序执行一个漫长的过程。

我正在新行上打印出完成百分比,每完成 1%

如何让程序在控制台窗口的同一位置打印出完成百分比?

I have a console app that performs a lengthy process.

I am printing out the percent complete on a new line, for every 1% complete.

How can I make the program print out the percent complete in the same location in the console window?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

属性 2024-08-11 01:51:17

打印 \r 以返回到行的开头(但不要打印换行符!)

例如:

using System;
using System.Threading;

class Test
{
    static void Main()
    {
        for (int i=0; i <= 100; i++)
        {
            Console.Write("\r{0}%", i);
            Thread.Sleep(50);
        }
    }
}

Print \r to get back to the start of the line (but don't print a newline!)

For example:

using System;
using System.Threading;

class Test
{
    static void Main()
    {
        for (int i=0; i <= 100; i++)
        {
            Console.Write("\r{0}%", i);
            Thread.Sleep(50);
        }
    }
}
一曲爱恨情仇 2024-08-11 01:51:17

您可以使用:

Console.SetCursorPosition();

适当地设置位置。

就像这样:

Console.Write("Hello : ");
for(int k = 0; k <= 100; k++){
    Console.SetCursorPosition(8, 0);
    Console.Write("{0}%", k);
    System.Threading.Thread.Sleep(50);
}

Console.Read();

You may use:

Console.SetCursorPosition();

To set the position appropriately.

Like so:

Console.Write("Hello : ");
for(int k = 0; k <= 100; k++){
    Console.SetCursorPosition(8, 0);
    Console.Write("{0}%", k);
    System.Threading.Thread.Sleep(50);
}

Console.Read();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文