写一个Java爱心程序
时间: 2023-03-01 10:36:14 浏览: 25
下面是一个简单的 Java 程序,用于在控制台上输出爱心字符图形:
```
public class Heart {
public static void main(String[] args) {
System.out.println("\u2665\u2665\u2665\u2665\u2665");
System.out.println("\u2665\u0020\u0020\u0020\u2665");
System.out.println("\u2665\u0020\u2665\u0020\u2665");
System.out.println("\u2665\u0020\u0020\u0020\u2665");
System.out.println("\u2665\u2665\u2665\u2665\u2665");
}
}
```
运行该程序后,您将在控制台上看到爱心形的字符图形。
相关问题
利用JAVA 写一个爱心程序
以下是一个简单的JAVA程序,可以输出一个爱心形状:
```java
public class Love {
public static void main(String[] args) {
for (double y = 1.5; y > -1.5; y -= 0.1) {
for (double x = -1.5; x < 1.5; x += 0.05) {
double a = x * x + y * y - 1;
System.out.print(a * a * a - x * x * y * y * y <= 0.0 ? "♥" : " ");
}
System.out.println();
}
}
}
```
运行程序,就会输出一个爱心形状:
```
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
```
用Java写一个爱心程序
好的,以下是用Java写一个爱心程序的示例代码:
```
public class LoveHeart {
public static void main(String[] args) {
int x, y, size = 20;
double d;
for (y = size; y >= -size; y--) {
for (x = -size; x <= size; x++) {
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();
}
}
}
```
这段程序通过嵌套的for循环和数学计算实现了一个爱心的图案。在控制台中运行该程序即可看到输出的爱心图案。
阅读全文