开发者

update the console window with java

开发者 https://www.devze.com 2023-02-05 07:42 出处:网络
How can i make java update the current console window instead of going onto a new line (or appending new content onto old).

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 progres开发者_运维问答s, 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消