java爱心代码编程
时间: 2023-11-08 07:05:38 浏览: 73
java爱心代码.zip
以下是 Java 实现爱心图案的代码:
```
public class Heart {
public static void main(String[] args) {
double x, y, size;
char[][] shape = new char[20][70];
for (int i = 0; i < shape.length; i++) {
for (int j = 0; j < shape[i].length; j++) {
shape[i][j] = ' ';
}
}
for (double theta = 0; theta < Math.PI; theta += 0.01) {
x = 16 * Math.pow(Math.sin(theta), 3);
y = 13 * Math.cos(theta) - 5 * Math.cos(2 * theta) - 2 * Math.cos(3 * theta) - Math.cos(4 * theta);
x += 35;
y += 10;
size = Math.sin(5 * theta) * 2 + 8;
int iX = (int) x;
int iY = (int) y;
if (iY >= 0 && iY < shape.length && iX >= 0 && iX < shape[iY].length) {
shape[iY][iX] = '*';
}
}
for (int i = 0; i < shape.length; i++) {
for (int j = 0; j < shape[i].length; j++) {
System.out.print(shape[i][j]);
}
System.out.println();
}
}
}
```
阅读全文