用java打印空心菱形

//打印空心菱形(逻辑思路就是先打印出实心菱形,再在控制星星格数的循环中找出最大值和最小值)
/*                      逻辑思路:逻辑思路就是先打印出实心菱形,再在控制星星格数的循环中找出最大值和最小值
     *
    * *
   *   *
  *     *
 *       *
*         *
 *       *
  *     *
   *   *
    * *
     *
*/
public class HollowDiamond {
           public static void main(String[] args) {
                    // 上半部分
                   for (int i = 1; i <= 6; i++) {
                             // 空格部分
                            for (int j = 5; j >= i; j--) {
                                     System.out.print(" ");
                            }
                            // 星星部分
                            for (int k = 1; k <= 2 * i - 1; k++) {
                                     if (k == 1 || k == 2 * i - 1) {
                                                    System.out.print("*");
                                      } else {
                                                    System.out.print(" ");
                                      }
                                  // System.out.print("*");
                             }
                    // 换行
                     System.out.println();
                  }

                 // 下半部分
                for (int i = 5; i >= 1; i--) {
                        // 空格部分
                       for (int j = 5; j >= i; j--) {
                                     System.out.print(" ");
                        }
                      // 星星部分
                      for (int k = 1; k <= 2 * i - 1; k++) {
                                        if (k == 1 || k == 2 * i - 1) {
                                                   System.out.print("*");
                                        } else {
                                                   System.out.print(" ");
                                        }
                     }
                  // 换行
                System.out.println();
           }
     }
}

你可能感兴趣的:(用java打印空心菱形)