Java打印金字塔(正实心、正空心,倒实心、倒空心),菱形

package com.cy.basic;

/**

  • n表示打印几行

  • i控制每一行

  • j控制*的个数

  • k控制空格数

  • 解题就干一件事:找j,k与i的关系(规律)
    */
    public class PrintPyramid {

    public static void main(String[] args) {
    int n = 6;
    printPyramid(n); //打印实心金字塔
    System.out.println("------------------------------------------");
    printReversedPyramid(n); //打印倒的实心金字塔
    System.out.println("------------------------------------------");
    printHollowPyramid(n); //打印空心金字塔
    System.out.println("------------------------------------------");
    printReversedHollowPyramid(n); //打印倒的空心金字塔
    System.out.println("------------------------------------------");
    printRhombus(n); //打印实心菱形
    System.out.println("------------------------------------------");
    printHollowRhombus(n); //打印空心菱形
    }

    /*
    打印正的实心金字塔
    *
    ***
    *****
    *******
    /
    private static void printPyramid(int n) {
    for (int i = 1; i <= n ; i++) {
    for (int k = 1;k <= n - i;k++) {
    System.out.print(" ");
    }
    for (int j = 1;j <= 2 * i - 1;j++) {
    System.out.print("
    ");
    }
    System.out.println();
    }
    }

    /*
    打印倒的实心金字塔
    *******
    *****
    ***
    *
    /
    private static void printReversedPyramid(int n) {
    for (int i = 1; i <= n ; i++) {
    for (int k = 1;k < i;k++) {
    System.out.print(" ");
    }
    /

    先死后活法找规律
    先死:
    n = 4; 2 * n - 1
    i 1 2 3 4
    *个数 7 5 3 1
    规律:7 + (i - 1) * (-2)
    后活:
    (2 * n - 1) + (i - 1) * (-2) = 2 * n - 2 * i + 1
    /
    for (int j = 1;j <= 2 * n - 2 * i + 1;j++) {
    System.out.print("
    ");
    }
    System.out.println();
    }
    }

    /*
    打印正的空心金字塔
    *
    * *
    * *
    *******
    /
    private static void printHollowPyramid(int n) {
    for (int i = 1; i <= n ; i++) {
    if (i == 1) {
    for (int k = 1;k <= n - 1;k++) {
    System.out.print(" ");
    }
    System.out.println("
    ");
    } else if (i == n) {
    for (int j = 1; j <= 2 * n - 1 ; j++) {
    System.out.print("");
    }
    System.out.println();
    } else {
    for (int k = 1;k <= n - i;k++) {
    System.out.print(" ");
    }
    System.out.print("
    ");
    for (int k = 1; k <= 2 * i - 3 ; k++) {
    System.out.print(" ");
    }
    System.out.print("*");
    System.out.println();
    }
    }
    }

    /*
    打印倒的空心金字塔
    *******
    * *
    * *
    *
    /
    private static void printReversedHollowPyramid(int n) {
    for (int i = 1; i <= n; i++) {
    if (i == 1) {
    for (int j = 1; j <= 2 * n - 1 ; j++) {
    System.out.print("
    ");
    }
    System.out.println();
    } else if (i == n) {
    for (int k = 1;k <= n - 1;k++) {
    System.out.print(" ");
    }
    System.out.println("");
    } else {
    for (int k = 1;k <= i - 1;k++) {
    System.out.print(" ");
    }
    System.out.print("
    ");
    for (int k = 1; k <= 2 * n - 2 * i - 1 ; k++) {
    System.out.print(" ");
    }
    System.out.print("*");
    System.out.println();

         }
     }
    

    }

    /*
    打印实心菱形
    *
    ***
    *****
    *******
    *****
    ***
    *
    /
    private static void printRhombus(int n) {
    for (int i = 1; i <= n - 1 ; i++) {
    for (int k = 1;k <= n - i;k++) {
    System.out.print(" ");
    }
    for (int j = 1;j <= 2 * i - 1;j++) {
    System.out.print("
    ");
    }
    System.out.println();
    }
    printReversedPyramid(n);
    }

    /*
    打印空心菱形
    *
    * *
    * *
    * *
    * *
    * *
    *
    /
    private static void printHollowRhombus(int n) {
    for (int i = 1; i <= n - 1 ; i++) {
    if (i == 1) {
    for (int k = 1;k <= n - 1;k++) {
    System.out.print(" ");
    }
    System.out.println("
    ");
    } else if (i == n) {
    for (int j = 1; j <= 2 * n - 1 ; j++) {
    System.out.print("");
    }
    System.out.println();
    } else {
    for (int k = 1;k <= n - i;k++) {
    System.out.print(" ");
    }
    System.out.print("
    ");
    for (int k = 1; k <= 2 * i - 3 ; k++) {
    System.out.print(" ");
    }
    System.out.print("*");
    System.out.println();
    }
    }

     for (int i = 1; i <= n; i++) {
         if (i == 1) {
             System.out.print("*");
             for (int k = 1; k <= 2 * n - 3 ; k++) {
                 System.out.print(" ");
             }
             System.out.print("*");
             System.out.println();
         } else if (i == n) {
             for (int k = 1;k <= n - 1;k++) {
                 System.out.print(" ");
             }
             System.out.println("*");
         } else {
             for (int k = 1;k <= i - 1;k++) {
                 System.out.print(" ");
             }
             System.out.print("*");
             for (int k = 1; k <= 2 * n - 2 * i - 1 ; k++) {
                 System.out.print(" ");
             }
             System.out.print("*");
             System.out.println();
    
         }
     }
    

    }

}

你可能感兴趣的:(Java打印金字塔(正实心、正空心,倒实心、倒空心),菱形)