(Java)试图创建一个基于文本的基本进度条,但输出不一致
只是想弄明白为什么控制台中的输出只在循环的大约10次迭代中更新一次,而不是一致地更新
public static void main(String[] args) throws Exception
{
for(int i = 0; i <=100;i++)
{
System.out.print(i+"% complete");
Thread.sleep(500);
System.out.print("\r");
}
}
}```
# 1 楼答案
也许可以尝试用
System.out.print("\n");
替换System.out.print("\r");
,以确保缓冲区刷新如果
\r
很重要,可以在末尾调用System.out.flush()
,以确保文本打印到输出