Command line progress bar in Java

前端 未结 15 2007
[愿得一人]
[愿得一人] 2020-11-28 01:04

I have a Java program running in command line mode. I would like to display a progress bar, showing the percentage of job done. The same kind of progress bar you would see u

15条回答
  •  渐次进展
    2020-11-28 01:36

    public class ProgressBar
    {
        private int max;
    
        public ProgressBar(int max0) {
            max = max0;
            update(0);
        }
    
        public void update(int perc) {
            String toPrint = "|";
            for(int i = 0; i < max; i++) {
                if(i <= (perc + 1))
                    toPrint += "=";
                else
                    toPrint += " ";
            }
    
            if(perc >= max)
                Console.print("\r");
            else
                Console.print(toPrint + "|\r");
        }
    }
    

提交回复
热议问题