代码:
package homework;
public class HomeWork7 {
public static void main(String[] args) {
for (int i = 0; i < 7; i++) {
if (i <= 3) {
/* 0: 3
* 1: 2
* 2: 1
* 3: 0*/
for (int x = 0; x < (3-i); x++) {
System.out.print(" ");
}
/* 0: 1
* 1: 3
* 2: 5
* 3: 7*/
for (int y = 0; y < (2 * i + 1); y++) {
System.out.print("*");
}
}else {
/*4: 1
* 5: 2
* 6: 3*/
for (int x = 0; x < (i-3); x++) {
System.out.print(" ");
}
/*4: 5
* 5: 3
* 6: 1*/
for (int y = 0; y < 13 - 2 * i; y++) {
System.out.print("*");
}
}
System.out.println();
}
}
}
运行截图:
代码 :
package homework;
public class HomeWork8 {
public static void main(String[] args) {
for (int i = 0; i < 7; i++) {
if (i <= 3) {
/* 0: 3
* 1: 2
* 2: 1
* 3: 0*/
for (int x = 0; x < (3-i); x++) {
System.out.print(" ");
}
/* 0: 1
* 1: 3
* 2: 5
* 3: 7*/
for (int y = 0; y < (2 * i + 1); y++) {
//把不是首个和末个的*用空格代替
if (y == 0 || y == 2 * i) {
System.out.print("*");
}else{
System.out.print(" ");
}
}
}else {
/*4: 1
* 5: 2
* 6: 3*/
for (int x = 0; x < (i-3); x++) {
System.out.print(" ");
}
/*4: 5
* 5: 3
* 6: 1*/
for (int y = 0; y < 13 - 2 * i; y++) {
//把不是首个和末个的*用空格代替
if (y == 0 || y == 12 - 2 * i) {
System.out.print("*");
}else{
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
运行截图: