写一个爱心代码,java
时间: 2023-10-26 14:36:26 浏览: 62
public class Heart {
public static void main(String[] args) {
int rows = 6;
int columns = 7;
String[][] heart = new String[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
heart[i][j] = " ";
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if ((i == 0 && j % 3 != 0) || (i == 1 && j % 3 == 0) || (i - j == 2) || (i + j == 8)) {
heart[i][j] = "*";
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(heart[i][j]);
}
System.out.println();
}
}
}
//输出结果
* *
* *
* *
* *
* *
* *
阅读全文