用java更新控制台窗口 [英] update the console window with java
本文介绍了用java更新控制台窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何让java更新当前的控制台窗口,而不是去一个新的行(或附加新的内容到旧)。
例如,如果我想证明进度,我会输出 progress n
百分比。
显然我想要做的只是用当前百分比更新n。
<$ p $
解决方案 $ p>
public class Test {
public static void main(String [] args)throws InterruptedException {
for(int i = 0; i< 100; i ++){
System.out.print(\rThinking ...+ i);
System.out.flush();
Thread.sleep(100);
}
}
}
所有控制台当然(一些IDE可能会斗争),但任何服从回车(\r
)应该没问题。
How can i make java update the current console window instead of going onto a new line (or appending new content onto old).
As an example, if i wanted to demonstrate progress, i would output progress n
where n would be the given percentage.
Obviously what i would want to do is simply update n with the current percentage. Any help would be appreciated.
解决方案
Something like this?
public class Test {
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 100; i++) {
System.out.print("\rThinking... " + i);
System.out.flush();
Thread.sleep(100);
}
}
}
It's not guaranteed to work on all consoles of course (some IDEs may struggle), but any that obey "carriage return" (the "\r"
) should be okay.
这篇关于用java更新控制台窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文