C#控制台进度条编写

175次阅读
没有评论

共计 568 个字符,预计需要花费 2 分钟才能阅读完成。

using System;
using System.Threading;
class Program
{static void Main()
    {Console.WriteLine("正在安装...");
        int totalProgress = 50; // 进度条的总长度
        for (int i = 0; i <= totalProgress; i++)
        {DrawProgressBar(i, totalProgress);
            Thread.Sleep(100); // 模拟一些任务正在进行
        }
        Console.WriteLine("\n 安装完成!");
        Console.ReadLine();}
    static void DrawProgressBar(int progress, int total)
    {Console.Write("\r"); // 光标移动到行首
        int barLength = 30;
        int filledLength = (int)(((double)progress / total) * barLength);
        string progressBar = "[" + new string('#', filledLength) + new string('-', barLength - filledLength) + "]";
        Console.Write(progressBar + $"{progress * 100 / total}%");
    }
}
正文完
 0
XSTPLAN
版权声明:本站原创文章,由 XSTPLAN 于2024-06-03发表,共计568字。
转载说明:本站所有资源和文章版权归作者所有,未经授权禁止转载。如有转载或引用,请注明来源。
评论(没有评论)