Java输出一个爱心
时间: 2024-02-22 11:58:21 浏览: 23
以下是Java代码,可以输出一个爱心:
```java
public class Heart {
public static void main(String[] args) {
int x, y, a, b;
double c;
for (y = 10; y > -10; y--) {
for (x = -30; x < 30; x++) {
c = Math.pow(x * 0.04, 2) + Math.pow(y * 0.1, 2) - 1;
if (c * c * c - Math.pow(x * 0.04, 2) * Math.pow(y * 0.1, 3) <= 0) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
```
运行代码后,会输出一个由星号组成的爱心图案,效果如下:
```
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
```
这只是一个简单的实现,您也可以尝试使用其他方法来输出更加美观的爱心图案。