帮我用Java写一个爱心
时间: 2023-05-21 12:00:50 浏览: 95
好的,以下是Java代码实现爱心的效果:
```
public class Heart {
public static void main(String[] args) {
int x, y;
double size;
char[][] heart = new char[20][70];
for (int i = 0; i < heart.length; i++) {
for (int j = 0; j < heart[i].length; j++) {
heart[i][j] = ' ';
}
}
for (double t = 0; t < 2 * Math.PI; t += 0.01) {
size = Math.sin(t);
x = (int) (15 + 10 * size);
y = (int) (13 - 7 * Math.cos(t));
heart[y][x] = '*';
x = (int) (15 + 10 * size);
y = (int) (13 - 7 * Math.cos(t + 0.01));
heart[y][x] = '*';
}
for (int i = 0; i < heart.length; i++) {
for (int j = 0; j < heart[i].length; j++) {
System.out.print(heart[i][j]);
}
System.out.println();
}
}
}
```
这段代码可以在控制台中输出一个爱心形状,你可以尝试运行一下看看效果。
阅读全文