有什么办法可以让程序在同一行重复自己,同时也擦除之前写的内容?
Is there any way to make a program that repeats itself on the same row, while also erasing what it wrote before?
我正在尝试编写一个程序,使 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();
}
}
}
我已经试过了,但不太行...
您可以打印退格键 \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
}
}
同时删除字符串中的新行,因为它会导致点打印在不同的行上。
我正在尝试编写一个程序,使 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();
}
}
}
我已经试过了,但不太行...
您可以打印退格键 \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
}
}
同时删除字符串中的新行,因为它会导致点打印在不同的行上。