编写一个动态的java爱心代码
时间: 2023-10-17 12:05:58 浏览: 112
以下是一个简单的动态 Java 爱心代码:
```
import java.util.concurrent.TimeUnit;
public class DynamicHeart {
public static void main(String[] args) throws InterruptedException {
while (true) {
System.out.println(" ♥♥ ♥♥");
System.out.println(" ♥ ♥ ♥ ♥");
System.out.println(" ♥ ♥ ♥ ♥");
System.out.println("♥ ♥ ♥");
System.out.println("♥ ♥");
System.out.println(" ♥ ♥");
System.out.println(" ♥ ♥");
System.out.println(" ♥ ♥");
System.out.println(" ♥ ♥");
System.out.println(" ♥ ♥");
System.out.println(" ♥ ♥");
System.out.println(" ♥");
TimeUnit.SECONDS.sleep(1);
clearConsole();
System.out.println(" ♥♥ ♥♥");
System.out.println(" ♥ ♥ ♥ ♥");
System.out.println(" ♥ ♥ ♥ ♥");
System.out.println("♥ ♥ ♥");
System.out.println("♥ ♥");
System.out.println(" ♥ ♥");
System.out.println(" ♥ ♥");
System.out.println(" ♥ ♥");
System.out.println(" ♥ ♥");
System.out.println(" ♥ ♥");
System.out.println(" ♥ ♥");
TimeUnit.SECONDS.sleep(1);
clearConsole();
}
}
public static void clearConsole() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
}
```
这个代码会在控制台上动态地输出一个爱心,并且每秒钟会清空控制台并重新输出。你可以自己尝试运行一下。
阅读全文