Java-用星号打印菱形

打印如图所示菱形9行9列(提示可以将菱形分成上下两个三角形,分析每行空格数和星号个数的关系)

代码如下:

 1 package com.homework.lhh;
 2 
 3 public class Ex20 {
 4 public static void main(String[] args) {
 5 for(int i = 1; i <= 5; i++){
 6 for(int j = 1; j <= 5 - i; j++){
 7 System.out.print(" ");
 8 }
 9 for(int j = 1; j <= 2 * i - 1; j++){
10 System.out.print("*");
11 }
12 System.out.println();
13 }
14 for(int i = 4; i > 0; i--){
15 for(int j = 1; j <= 5 - i; j++){
16 System.out.print(" ");
17 }
18 for(int j = 1; j <= 2 * i - 1; j++){
19 System.out.print("*");
20 }
21 System.out.println();
22 }
23 }
24 }


运行结果如图:

猜你喜欢

转载自www.cnblogs.com/coder-wf/p/12185193.html