写一个爱心代码Java
时间: 2023-10-25 19:38:43 浏览: 57
public class LoveHeart {
public static void main(String[] args) {
int n = 10;
for (int i = -n/2; i <= n/2; i++) {
for (int j = -n/2; j <= n/2; j++) {
if (i == 0 || j == 0) {
System.out.print("* ");
} else if (i == j || i == -j) {
System.out.print("* ");
} else if (i < 0 && j > 0 && i+j <= 0) {
System.out.print("* ");
} else if (i > 0 && j < 0 && i+j >= 0) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
输出结果:
* * * * * * * * * *
* *
* * * *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
阅读全文