C#命令行输出进度怎么写
要在C#命令行输出进度,可以使用Console类的Write和WriteLine方法。
以下是一个简单的示例,演示如何在命令行输出进度条:
using System;
class Program
{
static void Main()
{
// 设置进度条长度
int totalLength = 20;
for (int i = 0; i <= totalLength; i++)
{
// 清除当前行的内容
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
// 计算进度百分比
int progress = (int)((float)i / totalLength * 100);
// 输出进度条
Console.Write("[");
Console.Write(new string('#', i));
Console.Write(new string(' ', totalLength - i));
Console.Write("] {0}%", progress);
// 延迟一段时间
System.Threading.Thread.Sleep(100);
}
}
}
在上面的示例中,我们使用了Console.SetCursorPosition方法将光标定位到当前行的开头,然后使用Console.Write方法输出进度条的内容。最后,使用System.Threading.Thread.Sleep方法延迟一段时间,以模拟进度条的更新
原文地址: https://www.cveoy.top/t/topic/h57u 著作权归作者所有。请勿转载和采集!