如何使用Java在框架上打印Diamond

时间:2018-09-29 19:44:05

标签: java android

你好,我仍然是一个学习者,我面临着编写一个Java代码,该代码用一个包围钻石的框架打印钻石。我已经尝试过制作框架,但是在形成钻石方面遇到了挑战。 Here is a sample of how the diamond should look like

这是我的代码示例:

static int diagonalDifference(int[][] arr) {
    int left=0,right=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i==j){
                left=left+arr[i][j];
            }
        }
    }
    for(int i=0,int j=n;i<n;i++,j--){
        right=right+arr[i][j];
    }


}

}

3 个答案:

答案 0 :(得分:0)

这里是:

private void drawDimond(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("+");
    }

有什么问题吗?

答案 1 :(得分:0)

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

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
++  +--+ +----+ +------+ +--------+ +----------+ +------------+ +--------------+
    |<>| | /\ | |  /\  | |   /\   | |    /\    | |     /\     | |      /\      |
    +--+ |<-->| | /--\ | |  /--\  | |   /--\   | |    /--\    | |     /--\     |
         | \/ | |<====>| | /====\ | |  /====\  | |   /====\   | |    /====\    |
         +----+ | \--/ | |<------>| | /------\ | |  /------\  | |   /------\   |
                |  \/  | | \====/ | |<========>| | /========\ | |  /========\  |
                +------+ |  \--/  | | \------/ | |<---------->| | /----------\ |
                         |   \/   | |  \====/  | | \========/ | |<============>|
                         +--------+ |   \--/   | |  \------/  | | \----------/ |
                                    |    \/    | |   \====/   | |  \========/  |
                                    +----------+ |    \--/    | |   \------/   |
                                                 |     \/     | |    \====/    |
                                                 +------------+ |     \--/     |
                                                                |      \/      |
                                                                +--------------+

另见:Print a diamond shape with Java

答案 2 :(得分:0)

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
}

另见:How to output an ASCII star patterns in a table format?