如何使用 Java 打印带边框的钻石?

How to print a diamond with a frame using Java?

你好,我还是个学习者,我面临着编写一个 java 代码来打印一个钻石的框架,该钻石包含一个框架。我试过制作框架,但我很难制作钻石。

这是钻石的外观示例:

这是我的代码示例:

System.out.print("\n");

// for the top cover
System.out.print("+");
for (int i = 0; i <= (size * 2); i++) {
    System.out.print("-");
}
System.out.println("+");

// for the side 
int count = 0;
for (int i = 1; i <= (size * 2) - 1; i++) {
    System.out.print("|");
    for (int j = 0; j <= (size * 2); j++) {
        System.out.print(" ");
    }
    System.out.println("|");
}

// For the bottom
System.out.print("+");
for (int i = 0; i <= (size * 2); i++) {
    System.out.print("-");
}
System.out.println("+");

这里是:

public static void main(String[] args) {
    drawDiamond(6);
}

private static void drawDiamond(int size) {
    System.out.print("\n");

    // for the top cover
    System.out.print("+");
    for (int i = 0; i < size * 2 - 1; i++) {
        System.out.print("-");
    }
    System.out.println("+");

    //first half
    for (int i = 1; i < size; i++) {
        System.out.print("|");
        for (int j = 0; j < size - i; j++) {
            System.out.print("-");
        }
        for (int k = 0; k < i * 2 - 1; k++) {
            System.out.print("*");
        }
        for (int j = 0; j < size - i; j++) {
            System.out.print("-");
        }
        System.out.println("|");
    }

    //middle line
    System.out.print("|");
    for (int i = 0; i < size * 2 - 1; i++) {
        System.out.print("*");
    }
    System.out.println("|");

    //second half
    for (int i = 1; i < size; i++) {
        System.out.print("|");
        for (int j = 0; j <= (i * 2 - 1) / 2; j++) {
            System.out.print("-");
        }
        for (int k = 0; k < (size - i) * 2 - 1; k++) {
            System.out.print("*");
        }
        for (int j = 0; j <= (i * 2 - 1) / 2; j++) {
            System.out.print("-");
        }
        System.out.println("|");
    }

    // For the bottom
    System.out.print("+");
    for (int i = 0; i < size * 2 - ((size + 1) % 2); i++) {
        System.out.print("-");
    }
    System.out.println("+");
}

输出:

+-----------+
|-----*-----|
|----***----|
|---*****---|
|--*******--|
|-*********-|
|***********|
|-*********-|
|--*******--|
|---*****---|
|----***----|
|-----*-----|
+-----------+

有什么问题吗?

-nn 的两个嵌套 for 循环。零点在菱形的中心。

Try it online!

public static void main(String[] args) {
    for (int i = 0; i <= 7; i++)
        printDiamond(i);
}
static void printDiamond(int n) {
    System.out.println("n=" + n);
    for (int i = -n; i <= n; i++) {
        for (int j = -n - 1; j <= n + 1; j++)
            // skip middle vertical
            if (j == 0) System.out.print("");
            // borders:
            // vertical borders
            else if (Math.abs(j) == n + 1)
                if (Math.abs(i) == n) System.out.print("+");
                else System.out.print("|");
            // horizontal borders
            else if (Math.abs(i) == n) System.out.print("-");
            // rhombus:
            // left middle horizontal ending
            else if (i == 0 && j == -n) System.out.print("<");
            // right middle horizontal ending
            else if (i == 0 && j == n) System.out.print(">");
            // upper right and lower left borders
            else if (Math.abs(i - j) == n) System.out.print("\");
            // upper left and lower right borders
            else if (Math.abs(i + j) == n) System.out.print("/");
            // inner rhombus lines
            else if (Math.abs(i) + Math.abs(j) < n)
                // double central line for odd n
                if ((n - i) % 2 != 0) System.out.print("=");
                else System.out.print("-");
            // whitespace
            else System.out.print(" ");
        // new line
        System.out.println();
    }
}

输出(合并):

n=0 n=1  n=2    n=3      n=4        n=5          n=6            n=7
++  +--+ +----+ +------+ +--------+ +----------+ +------------+ +--------------+
    |<>| | /\ | |  /\  | |   /\   | |    /\    | |     /\     | |      /\      |
    +--+ |<-->| | /--\ | |  /--\  | |   /--\   | |    /--\    | |     /--\     |
         | \/ | |<====>| | /====\ | |  /====\  | |   /====\   | |    /====\    |
         +----+ | \--/ | |<------>| | /------\ | |  /------\  | |   /------\   |
                |  \/  | | \====/ | |<========>| | /========\ | |  /========\  |
                +------+ |  \--/  | | \------/ | |<---------->| | /----------\ |
                         |   \/   | |  \====/  | | \========/ | |<============>|
                         +--------+ |   \--/   | |  \------/  | | \----------/ |
                                    |    \/    | |   \====/   | |  \========/  |
                                    +----------+ |    \--/    | |   \------/   |
                                                 |     \/     | |    \====/    |
                                                 +------------+ |     \--/     |
                                                                |      \/      |
                                                                +--------------+

另请参阅:

ASCII艺术钻石展

要在一行中获得菱形的组合输出,您可以使用三个嵌套的 for 循环 和几个 if else 语句 .

n=1  n=2    n=3      n=4        n=5          n=6            n=7              
+--+ +----+ +------+ +--------+ +----------+ +------------+ +--------------+ 
|<>| | /\ | |  /\  | |   /\   | |    /\    | |     /\     | |      /\      | 
+--+ |<-->| | /--\ | |  /--\  | |   /--\   | |    /--\    | |     /--\     | 
     | \/ | |<====>| | /====\ | |  /====\  | |   /====\   | |    /====\    | 
     +----+ | \--/ | |<------>| | /------\ | |  /------\  | |   /------\   | 
            |  \/  | | \====/ | |<========>| | /========\ | |  /========\  | 
            +------+ |  \--/  | | \------/ | |<---------->| | /----------\ | 
                     |   \/   | |  \====/  | | \========/ | |<============>| 
                     +--------+ |   \--/   | |  \------/  | | \----------/ | 
                                |    \/    | |   \====/   | |  \========/  | 
                                +----------+ |    \--/    | |   \------/   | 
                                             |     \/     | |    \====/    | 
                                             +------------+ |     \--/     | 
                                                            |      \/      | 
                                                            +--------------+ 

Try it online!

// maximum diamond size
int max = 7;
// title row
for (int n = 1; n <= max; n++) {
    System.out.print("n=" + n);
    // padding up to the diamond width
    for (int a = 0; a < n * 2 - 1; a++)
        System.out.print(" ");
    // indent after diamond
    System.out.print(" ");
}
System.out.println();
// combined rows of diamonds
for (int row = 1; row <= max * 2 + 1; row++) {
    // sizes of diamonds
    for (int n = 1; n <= max; n++) {
        // current diamond row [-n, n]
        int i = row - n - 1;
        if (i <= n) // elements of the row [-n-1, n+1]
            for (int j = -n - 1; j <= n + 1; j++)
                if (j == 0) continue; // skip middle vertical
                else if (Math.abs(j) == n + 1) // vertical borders & corners
                    System.out.print(Math.abs(i) == n ? "+" : "|");
                else if (Math.abs(i) == n) // horizontal borders
                    System.out.print("-");
                else if (i == 0 && Math.abs(j) == n) // middle left & right tips
                    System.out.print(j == -n ? "<" : ">");
                else if (Math.abs(i - j) == n) // upper right & lower left edges
                    System.out.print("\");
                else if (Math.abs(i + j) == n) // upper left & lower right edges
                    System.out.print("/");
                else if (Math.abs(i) + Math.abs(j) < n) // inner rhombus lines
                    System.out.print((n - i) % 2 != 0 ? "=" : "-");
                else // whitespace
                    System.out.print(" ");
        else // empty row, current diamond width + borders
            for (int a = 0; a < n * 2 + 2; a++)
                System.out.print(" ");
        // indent after diamond
        System.out.print(" ");
    }
    System.out.println(); // new line
}

另请参阅: