java控制台输出百分比进度条示例

时间:2022-05-04 21:57:39

System.out.print("\b") 会在控制台下往回删掉一个字符,如果你想回删多个字符就打印多个 "\b"

 

复制代码代码如下:

public static void main(String[] args) throws Exception {
    System.out.print("Progress:");
    for (int i = 1; i <= 100; i++) {
        System.out.print(i + "%");
        Thread.sleep(100);

        for (int j = 0; j <= String.valueOf(i).length(); j++) {
            System.out.print("\b");
        }
    }
    System.out.println();
}