java爱心代码会动的
时间: 2023-11-17 15:04:10 浏览: 50
以下是一个使用Java编写的动态爱心代码示例,它可以在控制台上动态显示一个爱心形状:
```java
public class DynamicHeart {
public static void main(String[] args) throws InterruptedException {
int size = 10;
while (true) {
for (int y = size; y >= -size; --y) {
for (int x = -size; x <= size; ++x) {
double d = Math.pow(Math.pow(x * 0.04, 2) + Math.pow(y * 0.1, 2) - 1, 3) - Math.pow(x * 0.04, 2) * Math.pow(y * 0.1, 3);
System.out.print(d <= 0.0 ? "❤️" : " ");
}
System.out.println();
}
Thread.sleep(100);
}
}
}
```
运行这段代码,您将看到一个在控制台上动态显示的爱心形状。您可以通过修改size的值来改变爱心的大小。
相关问题
java 会动爱心代码
以下是Java实现的爱心代码:
```
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class HeartAnimation extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 600;
private static final int HEIGHT = 600;
private int x;
private int y;
private int size;
private Color color;
public HeartAnimation() {
x = WIDTH / 2;
y = HEIGHT / 2;
size = 50;
color = Color.RED;
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(color);
g.fillOval(x - size / 2, y - size / 2, size, size);
g.fillOval(x - size / 2 + size, y - size / 2, size, size);
int[] xPoints = { x - size / 2, x + size / 2, x };
int[] yPoints = { y - size / 2, y - size / 2, y + size };
g.fillPolygon(xPoints, yPoints, 3);
}
public void run() {
while (true) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
size += 5;
color = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
repaint();
if (size > WIDTH) {
size = 50;
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Heart Animation");
HeartAnimation heart = new HeartAnimation();
frame.add(heart);
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Thread thread = new Thread(heart);
thread.start();
}
}
```
运行代码,将会看到一个不断变换颜色和大小的爱心。
java爱心代码动态名字
以下是Java爱心代码动态名字:
```java
import java.util.Scanner;
public class HeartName {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入您的名字:");
String name = sc.next();
int length = name.length();
// 生成空格字符串
String space = "";
for (int i = 1; i <= length + 2; i++) {
space += " ";
}
// 打印心形上半部分
for (int i = length / 2 + 1; i > 0; i--) {
for (int j = 1; j <= i * 2 - 2; j++) {
System.out.print("*");
}
int spaceNum = length * 2 - i * 4 + 2;
for (int j = 1; j <= spaceNum; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i * 2 - 2; j++) {
System.out.print("*");
}
System.out.println();
}
// 打印心形下半部分
for (int i = 1; i <= length / 2 + 1; i++) {
for (int j = 1; j <= 2 * i - 2; j++) {
System.out.print("*");
}
int spaceNum = length * 4 - i * 4 + 2;
for (int j = 1; j <= spaceNum; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 2; j++) {
System.out.print("*");
}
System.out.println();
}
// 打印名字
for (int i = 1; i <= length / 2 + 1; i++) {
System.out.print("*");
}
System.out.print(" " + name + " ");
for (int i = 1; i <= length / 2 + 1; i++) {
System.out.print("*");
}
}
}
```
使用这个代码,用户可以输入自己的名字,程序会在心形图案中显示出该名字。例如,如果用户输入“Tom”,则会输出以下内容:
```
请输入您的名字:Tom
********** **********
**** **** **** ****
*** *** *** ***
*** *** *** ***
*** *** *** ***
*** *** *** ***
**** **** **** ****
********** **********
**** ****
**** Tom ****
**** ****
```