帮我写一个java爱心代码
时间: 2023-10-26 09:46:01 浏览: 97
public class Heart {
public static void main(String[] args) {
int size = 10;
int[][] heart = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
heart[i][j] = 0;
}
}
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if ((i <= size / 2 && j >= size / 2 - i && j <= size / 2 + i) ||
(i > size / 2 && j >= i - size / 2 && j <= size - (i - size / 2))) {
heart[i][j] = 1;
}
}
}
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (heart[i][j] == 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
阅读全文
相关推荐















