chapter-4-练习题-应用for循环打印菱形
2020-04-08 本文已影响0人
一只狗被牵着走
密实的菱形
效果
*
***
*****
*******
*********
*******
*****
***
*
代码
public static void main(String[] args) {
int max = 6;
for (int i = 0; i < max; i++){
for (int j = max - 1; j > i; j--){
System.out.print(" ");
}
for (int j = 0; j < 2 * i - 1; j++){
System.out.print("*");
}
System.out.print("\n");
}
for (int i = max - 2; i > 0; i--){
for (int j = i; j < max - 1; j++){
System.out.print(" ");
}
for (int j = 2 * i - 1; j > 0; j--){
System.out.print("*");
}
System.out.print("\n");
}
}
间隔的菱形
效果
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
代码
public static void main(String[] args) {
int max = 6;
for (int i = 0; i < max; i++){
for (int j = max - 1; j > i; j--){
System.out.print(" ");
}
for (int j = 0; j < 2 * i - 1; j++){
if ((j + 1) % 2 != 0){
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.print("\n");
}
for (int i = max - 2; i > 0; i--){
for (int j = i; j < max - 1; j++){
System.out.print(" ");
}
for (int j = 2 * i - 1; j > 0; j--){
if (j % 2 != 0){
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.print("\n");
}
}