简体   繁体   English

如何在Java控制台应用程序中制作进度条?

[英]How to make a progress bar in Java console application?

I am trying to make a progress bar like this 我正在尝试制作这样的进度条

[#        ] 10%
[#####    ] 50%
[#########] 100%

What i tried 我尝试了什么

public static void main(String[] args) throws InterruptedException {
        String format = "[#          ]%d%%\r";
        for(int i=0;i<=100;i++){
            System.out.print(String.format(format, i));
            Thread.sleep(10);
        }
}

output: 输出:

[#        ] 10%
[#        ] 50%
[#        ] 100%

the problem is I can't increment the the # count according to the progress. 问题是我无法根据进度增加#计数。

So how can i move or increment the # ? 那么我如何移动或增加#号呢?

You need to write a piece of code that produces a ten-character String starting in # s and ending in spaces. 你需要写一段代码产生一个10字符String开始在# S和在空间中结束。 Pass this method a number from 0 to 100. The method should divide the number by ten, rounding the result up. 将此方法传递一个从0到100的数字。该方法应将数字除以10,然后将结果四舍五入。 This will give you the number of # characters in the ten-character bar: 这将在十个字符的栏中为您提供#字符的数量:

int numPounds = (pct + 9) / 10;

Make a loop that appends '#' numPounds times, and then appends ' ' until the length of the string is ten. 创建一个循环,该循环将'#' numPounds次附加,然后附加' '直到字符串的长度为十。 Print the result between [ ... ] characters to complete the exercise. [ ... ]字符之间打印结果以完成练习。

private static final StringBuilder res = new StringBuilder();;

static String progress(int pct) {
    res.delete(0, res.length());
    int numPounds = (pct + 9) / 10;
    for (int i = 0 ; i != numPounds ; i++) {
        res.append('#');
    }
    while (res.length() != 10) {
        res.append(' ');
    }
    return res.toString();
}

public static void main (String[] args) throws java.lang.Exception
{
    for (int i = 0 ; i <= 100 ; i++) {
        Thread.sleep(10);
        System.out.print(String.format("[%s]%d%%\r", progress(i), i));
    }
}

take a look at this 看看这个

static int current=0;
static int previous=0;
static String previousString="";
public static void main(String[] args) throws InterruptedException {
    String format = "[%s]%d%%\r";

    for (int i = 0; i <= 100; i++) {
        try {
            current=i/10;
            System.out.print(String.format(format, repeat("#",current ), i));
        } catch (ArithmeticException e) {
            System.out.print(String.format(format, repeat("#", 0), i));
        }
        Thread.sleep(10);
    }
}

static String repeat(String StringToRepat, int repetition) {
    if (repetition==previous)
    {
        return previousString;
    }
    StringBuilder builder = new StringBuilder("");
    for (int i = 0; i < repetition; i++)
        builder.append(StringToRepat);
    previousString=builder.toString();
    previous=repetition;
    return previousString;


}

All credits goes to @Poshemo 所有学分归@Poshemo

public static void main(String[] args) throws InterruptedException {
    final StringBuilder sb  =  new StringBuilder();
    String format = "[%-11s]%d%%\r";

    for(int i=0;i<=100;i++){
        if(i%10==0){
            sb.append("#");
        }
        System.out.print(String.format(format, sb, i));
        Thread.sleep(10);
    }
}

I needed a progress bar for a project, I just put it on Github in case it can help somebody. 我需要一个项目的进度条,我只是把它放在Github上 ,以防它可以帮助别人。

Basic idea is that your main task is divided into subtasks, you just notify the bar the advances of the subtask and the display is done automatically. 基本思想是将您的主要任务划分为多个子任务,您只需将该子任务的进度通知栏并自动完成显示即可。

Sample usage with randomized timings: 随机计时的样本用法:

ConsoleProgressBar bar = new ConsoleProgressBar(numberOfSubtasks);
bar.startAndDisplay();
for (int i = 0; i < numberOfSubtasks; i += increment) {
 bar.updateAndDisplay(increment);
 Thread.sleep(minrandomTime + rand.nextInt(maxrandomTime - minrandomTime));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM