Java跳动爱心代码,百分百还原跳动的爱心!
时间: 2023-11-20 18:56:04 浏览: 89
以下是Java实现跳动爱心代码的例子:
```java
import java.util.Scanner;
public class Heart {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入你想要的爱心大小(1-10):");
int size = sc.nextInt();
sc.close();
if (size < 1 || size > 10) {
System.out.println("请输入1-10之间的数字!");
return;
}
String[][] heart = new String[6 * size][6 * size];
for (int i = 0; i < heart.length; i++) {
for (int j = 0; j < heart[i].length; j++) {
double distance1 = Math.sqrt(Math.pow(i - heart.length / 2, 2) + Math.pow(j - heart[i].length / 2, 2));
double distance2 = Math.sqrt(Math.pow(i - heart.length / 2, 2) + Math.pow(j - heart[i].length / 2 - 1.5 * size * size, 2));
if (distance1 < 1.5 * size * size || distance2 < 1.5 * size * size) {
heart[i][j] = "❤️";
} else {
heart[i][j] = " ";
}
}
}
for (String[] row : heart) {
for (String col : row) {
System.out.print(col);
}
System.out.println();
}
}
}
```
阅读全文