Java程序实现进度条打印

//方法一(缺陷:在开发环境下可能显示模式是Unicode,然后\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("结束");
}
//方法二:
public static void main(String[] args) throws Exception {
    for (int i = 1; i <= 100; i++) {
	System.out.print('\r' + "Progress:" + i + "%");
	Thread.sleep(100);
    }
    System.out.println("结束");
}
知识点:
1.\b可在控制台删除一个字节的打印记录
2.\r可在控制台删除整行的打印记录

发表回复

您的电子邮箱地址不会被公开。

1 × 2 =