【问题标题】:Is there any way to make a program that repeats itself on the same row, while also erasing what it wrote before?有什么办法可以让程序在同一行重复,同时删除之前写的内容?
【发布时间】:2020-07-02 01:09:19
【问题描述】:

我正在尝试制作一个程序,使 3 个点“...”一个接一个出现,然后从同一行的开头开始;像这样:

Phase 1: .
Phase 2: ..
Phase 3: ...
Phase 4: .
Phase 5: ..

等等。

enter code here


    String text2 = "..." + "\n";
    for (int i = 0; i <= 3; i++) {

        for (int j = 0; j < text2.length(); j++) {
            System.out.print("" + text2.charAt(j));
            try {
                Thread.sleep(300);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }

        }
    }

我试过了,但效果不太好……

【问题讨论】:

标签: java delay loading


【解决方案1】:

你可以打印退格\b,只要点像这样:

public static void main(String[] args)
    {
        String text2 = "...";
        for (int i = 0; i <= 3; i++) 
        {
            for (int j = 0; j < text2.length(); j++) {
                System.out.print("" + text2.charAt(j));
                try {
                    Thread.sleep(300);
                } catch (InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }
            }
            System.out.print("\b".repeat(text2.length())); //Java 11
        }
    }

同时删除字符串中的新行,因为它会导致点打印在不同的行上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-28
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 2019-02-14
    相关资源
    最近更新 更多