【编程小技巧】

输出内容覆盖原有的内容

\r表示回车符,会将光标移至开始位置,同时使用非换行形式的print

public class Main {
    public static void main(String[] args) {
        Random r = new Random();
        String[] all = new String[10];
        for (int i = 0; i <= 100; i++) {
            try {
                Thread.sleep(r.nextInt(100));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            all[0] = i + "%";
            System.out.print("\r" +Arrays.toString(all));
        }
    }
}

image

posted @ 2024-11-27 23:33  chendsome  阅读(9)  评论(0)    收藏  举报