今天有一个控制台打印进度条的需求,研究了一下,原理很简单,利用\b输出退格键就行了
public class TerminalTest{
public static void main(String[] args) {
for (int i = 0; i <= 100; i++) {
printSchedule(i);
try {
(10);
} catch (InterruptedException e) {
();
}
}
}
/**
* 进度条总长度
*/
private static int TOTLE_LENGTH = 30;
public static void printSchedule(int percent){
for (int i = 0; i < TOTLE_LENGTH + 10; i++) {
("\b");
}
//░▒
int now = TOTLE_LENGTH * percent / 100;
for (int i = 0; i < now; i++) {
(">");
}
for (int i = 0; i < TOTLE_LENGTH - now; i++) {
(" ");
}
(" " + percent + "%");
}
}