Java 常用工具类(33) : 打印进度条


public class ProgressBarExample {
    public static void main(String[] args) throws Exception {
        Double total = 100.0; // 总进度
        Double current = 0.0; // 当前进度
        printProgressBar(current, total);
        for (int i = 0; i < 100; i++) {
            updateProgressBar(i * 1.0, total);
            Thread.sleep(100);
        }
    }


    public static void printProgressBar(Double current, Double total) {
        int percent = current.intValue() * 100 / total.intValue();
        int completed = current.intValue() * 20 / total.intValue();

        System.out.print("[");
        for (int i = 0; i < completed; i++) {
            System.out.print("=");
        }
        for (int i = completed; i < 20; i++) {
            System.out.print(" ");
        }
        System.out.print("] " + percent + "%");
    }

    public static void updateProgressBar(Double current, Double total) {
        System.out.print("\r"); // 回车,将光标移至行首
        printProgressBar(current, total);
    }
}

你可能感兴趣的:(#,Java,常用工具类,java,开发语言,打印进度条)