ITPub博客

首页 > 应用开发 > Java > Java实现在控制终端显示的字符进度条

Java实现在控制终端显示的字符进度条

Java 作者:壹頁書 时间:2014-05-16 10:29:45 0 删除 编辑
今天看到一个很有意思的小程序,记录一下


  1. import java.text.DecimalFormat;

  2. /**
  3.  * 控制台字符型进度条。
  4.  *
  5.  * @author 傲风 <aofengblog@163.com>
  6.  */
  7. public class ConsoleProgressBar {

  8.     private long minimum = 0; // 进度条起始值
  9.     
  10.     private long maximum = 100; // 进度条最大值
  11.     
  12.     private long barLen = 100; // 进度条长度
  13.     
  14.     private char showChar = '='; // 用于进度条显示的字符
  15.     
  16.     private DecimalFormat formater = new DecimalFormat("#.##%");
  17.     
  18.     /**
  19.      * 使用系统标准输出,显示字符进度条及其百分比。
  20.      */
  21.     public ConsoleProgressBar() {
  22.     }
  23.     
  24.     /**
  25.      * 使用系统标准输出,显示字符进度条及其百分比。
  26.      *
  27.      * @param minimum 进度条起始值
  28.      * @param maximum 进度条最大值
  29.      * @param barLen 进度条长度
  30.      */
  31.     public ConsoleProgressBar(long minimum, long maximum,
  32.             long barLen) {
  33.         this(minimum, maximum, barLen, '=');
  34.     }
  35.     
  36.     /**
  37.      * 使用系统标准输出,显示字符进度条及其百分比。
  38.      *
  39.      * @param minimum 进度条起始值
  40.      * @param maximum 进度条最大值
  41.      * @param barLen 进度条长度
  42.      * @param showChar 用于进度条显示的字符
  43.      */
  44.     public ConsoleProgressBar(long minimum, long maximum,
  45.             long barLen, char showChar) {
  46.         this.minimum = minimum;
  47.         this.maximum = maximum;
  48.         this.barLen = barLen;
  49.         this.showChar = showChar;
  50.     }
  51.     
  52.     /**
  53.      * 显示进度条。
  54.      *
  55.      * @param value 当前进度。进度必须大于或等于起始点且小于等于结束点(start <= current <= end)。
  56.      */
  57.     public void show(long value) {
  58.         if (value < minimum || value > maximum) {
  59.             return;
  60.         }
  61.         
  62.         reset();
  63.         minimum = value;
  64.         float rate = (float) (minimum*1.0 / maximum);
  65.         long len = (long) (rate * barLen);
  66.         draw(len, rate);
  67.         if (minimum == maximum) {
  68.             afterComplete();
  69.         }
  70.     }

  71.     private void draw(long len, float rate) {
  72.         for (int i = 0; i < len; i++) {
  73.             System.out.print(showChar);
  74.         }
  75.         System.out.print(' ');
  76.         System.out.print(format(rate));
  77.     }
  78.     
  79.     private void reset() {
  80.         System.out.print('\r');
  81.     }
  82.     
  83.     private void afterComplete() {
  84.         System.out.print('\n');
  85.     }
  86.     
  87.     private String format(float num) {
  88.         return formater.format(num);
  89.     }
  90.     
  91.     public static void main(String[] args) throws InterruptedException {
  92.         ConsoleProgressBar cpb = new ConsoleProgressBar(0, 100, 20, '=');
  93.         for (int i = 1; i <= 100; i++) {
  94.             cpb.show(i);
  95.             Thread.sleep(100);
  96.         }
  97.     }

  98. }
转自:
http://aofengblog.blog.163.com/blog/static/6317021201362723025426/

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1162488/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 学习Javascript闭包
请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339970