zoukankan      html  css  js  c++  java
  • C#控制台显示进度条

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ProgressBar
    {
        class Program
        {
            static void Main(string[] args)
            {
                ConsoleColor colorBack = Console.BackgroundColor;
                ConsoleColor colorFore = Console.ForegroundColor;
    
                //Console.SetCursorPosition(0, 0);
                Console.WriteLine("Loading...");
                ////绘制界面
                //Console.SetCursorPosition(0, 2);//指定输出位置
                //Console.WriteLine("*************** Acknowledged order ***************");
                //Console.BackgroundColor = ConsoleColor.DarkCyan;
                //for (int i = 0; ++i <= 50; )
                //{
                //    Console.Write(" ");
                //}
                //Console.SetCursorPosition(0, 3);
                //Console.WriteLine(" ");
                //Console.BackgroundColor = colorBack;
                //Console.SetCursorPosition(0, 4);
                //Console.WriteLine("0%");
                //Console.SetCursorPosition(0, 5);
                //Console.WriteLine("**************************************************");
    
                int count = 0;
                int index = 0;
                double prePercent = 0;
    
                List<string> list = new List<string>() { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
                count = list.Count;
                //Console.SetCursorPosition(0, 1);
                //Console.WriteLine("Total:" + count);
                //count = 0;
                System.Threading.Thread.Sleep(5000);//模拟加载等待
                if (count > 0)
                {
                    //Console.SetCursorPosition(0, 1);
                    Console.WriteLine("Total:" + count);
                    //绘制界面
                    Console.WriteLine("********************* Loading *********************"); 
                    Console.BackgroundColor = ConsoleColor.DarkCyan;
                    for (int i = 0; ++i <= 50; )
                    {
                        Console.Write(" ");
                    }
                    Console.WriteLine(" ");
                    Console.BackgroundColor = colorBack;
                    Console.WriteLine("0%");
                    Console.WriteLine("***************************************************");
    
                    foreach (string str in list)
                    {
                        #region 绘制界面
                        //绘制界面
                        index++;
                        double percent;
                        if (index <= count)
                        {
                            percent = (double)index / count;
                            percent = Math.Ceiling(percent * 100);
                        }
                        else
                        {
                            percent = 1;
                            percent = Math.Ceiling(percent * 100);
                        }
                        // 开始控制进度条和进度变化
                        for (int i = Convert.ToInt32(prePercent); i <= percent; i++)
                        {
                            //绘制进度条进度                 
                            Console.BackgroundColor = ConsoleColor.Yellow;//设置进度条颜色                
                            Console.SetCursorPosition(i / 2, 3);//设置光标位置,参数为第几列和第几行                
                            Console.Write(" ");//移动进度条                
                            Console.BackgroundColor = colorBack;//恢复输出颜色                
                            //更新进度百分比,原理同上.                
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.SetCursorPosition(0, 4);
                            Console.Write("{0}%", i);
                            Console.ForegroundColor = colorFore;
                            //模拟实际工作中的延迟,否则进度太快
                            System.Threading.Thread.Sleep(50);
                        }
                        prePercent = percent;
                        #endregion
                    }
    
                    Console.SetCursorPosition(0, 6);
    
                    Console.WriteLine("Loading Complete.");
                }
                Console.ReadLine();//会等待直到用户按下回车,一次读入一行
            }
        }
    }
    

    效果图:

    作者:长毛象
    本文版权归作者和博客园共有,个人学习成果,请多多指教,欢迎转载,请保留原文链接
  • 相关阅读:
    微信小程序,搜索结果关键词高亮 wxml不能动态识别html标签
    关于vue 全局loading方案
    element ui树样式问题
    Promise.all( ) 的使用
    关于form表单校验问题
    vue 异步改同步 获取结果 动态函数
    1.关于数据for循环不要用index作为key,2.面二次刷新404问题(空白) 的探讨 3. vue图片上传
    element ui点击切换皮肤
    关于element ui input、以及button样式不能覆盖的解决办法(登录页面)
    vue版本根据当前路由匹配到根父节点并且激活
  • 原文地址:https://www.cnblogs.com/xiangyisheng/p/6524610.html
Copyright © 2011-2022 走看看