0

I am trying to draw out a diamond in a frame. I figured my way through the top half, but when I come to the 2nd half I had attempted to invert the loops and problems came up. I played around switching operators just to see the result, but still nothing works. Please help. What am I not seeing.

// 1st Half of Diamond

// Creates Lines
for (int i = 1; i <= 3; i++) {
    if (i == 1) {
        System.out.print("+");
        for (int h = 1; h <= 8; h++) {
            System.out.print("-");
        }
        System.out.print("+" + "\n");
    }
    System.out.print("|");

    // Nested Loop Creates Spaces Left Side
    for (int j = 4; j > i; j--) {
        System.out.print(" ");
    }
    System.out.print("/");

    // Nested Loop Creates Values Inside
    for (int j = 1; j < i; j++) {
        if (i % 2 == 0) {
            System.out.print("--");
        } else if (i == 1) {
            System.out.print("\\");
        } else {
            System.out.print("==");
        }
    }
    System.out.print("\\");

    // Nested Loop Creates Spaces Right Side
    for (int j = 4; j > i; j--) {
        System.out.print(" ");
    }
    System.out.print("|");
    System.out.print("\n");
}

// Midpoint of Diamond
System.out.print("|<------>|" + "\n");

//============================
//****HERE PROBLEMS ARISE****

// 2nd Half of Diamond

// Creates Lines
for (int i = 1; i <= 3; i++) {
    System.out.print("|");

    // Nested Loop Creates Spaces Left Side
    for (int j = 1; j <= i; j++) {
        System.out.print(" ");
    }
    System.out.println("\\");

    // Nested Loop Creates Values Inside
    for (int j = 1; j < 2; j++) {
        System.out.print("+");
        for (int h = 1; h <= 8; h++) {
            System.out.print("-");
        }
        System.out.print("+" + "\n");
        if (i % 2 == 0) {
            System.out.print("-");
        } else if (i == 3) {
            System.out.print("/");
        } else {
            System.out.print("=");
        }
    }
}
Community
  • 1
  • 1
TAA
  • 59
  • 4

3 Answers3

0

I assume you're trying to achieve this result:

public class Diamond {
    public static void main(String[] args) {
        // 1st Half of Diamond
        // Creates Lines
        for (int i = 1; i <= 3; i++) {
            if (i == 1) {
                System.out.print("+");
                for (int h = 1; h <= 8; h++) {
                    System.out.print("-");
                }
                System.out.print("+" + "\n");
            }
            System.out.print("|");
            // Nested Loop Creates Spaces Left Side
            for (int j = 4; j > i; j--) {
                System.out.print(" ");
            }
            System.out.print("/");
            // Nested Loop Creates Values Inside
            for (int j = 1; j < i; j++) {
                if (i % 2 == 0) {
                    System.out.print("--");
                } else if (i == 1) {
                    System.out.print("\\");
                } else {
                    System.out.print("==");
                }
            }
            System.out.print("\\");
            // Nested Loop Creates Spaces Right Side
            for (int j = 4; j > i; j--) {
                System.out.print(" ");
            }
            System.out.print("|");
            System.out.print("\n");
        }
        // Midpoint of Diamond
        System.out.print("|<------>|" + "\n");
        // 2nd Half of Diamond
        // Creates Lines
        for (int i = 1; i <= 3; i++) {
            System.out.print("|");
            // Nested Loop Creates Spaces Left Side
            for (int j = 1; j <= i; j++) {
                System.out.print(" ");
            }
            System.out.print("\\");
            // Nested Loop Creates Values Inside
            for (int j = 1; j <= i; j++) {
                if (i == 2) {
                    System.out.print("-");
                } else if (i == 1) {
                    System.out.print("====");
                } else {
                    System.out.print("");
                }
            }
            System.out.print("/");
            // Nested Loop Creates Spaces Right Side
            for (int j = 0; j < i; j++) {
                System.out.print(" ");
            }
            System.out.println("|");
        }
        System.out.print("+");
        for (int h = 1; h <= 8; h++) {
            System.out.print("-");
        }
        System.out.print("+" + "\n");
    }
}

Output:

+--------+
|   /\   |
|  /--\  |
| /====\ |
|<------>|
| \====/ |
|  \--/  |
|   \/   |
+--------+
0

First off, I suggest keeping your code grouped neatly with regard to the output. It's just harder to see what blocks of code produce what out put when you don't. Here's a cleaned up version of the 1st half (but the code is exactly the same):

// 1st Half of Diamond
for (int i = 1; i <= 3; i++) {
    //TOP OR BOTTOM LINE
    {
        if (i == 1) {
            System.out.print("+");
            for (int h = 1; h <= 8; h++) {
                System.out.print("-");
            }
            System.out.print("+" + "\n");
        }
    }

    //INTERIOR LINES
    {
        System.out.print("|");
        // Nested Loop Creates Spaces Left Side
        for (int j = 4; j > i; j--) {
            System.out.print(" ");
        }
        System.out.print("/");
        // Nested Loop Creates Values Inside
        for (int j = 1; j < i; j++) {
            if (i % 2 == 0) {
                System.out.print("--");
            } else if (i == 1) {
                System.out.print("\\");
            } else {
                System.out.print("==");
            }
        }
        System.out.print("\\");
        // Nested Loop Creates Spaces Right Side
        for (int j = 4; j > i; j--) {
            System.out.print(" ");
        }
        System.out.print("|");
        System.out.print("\n");
    }
}

The output is as you get

+--------+
|   /\   |
|  /--\  |
| /====\ |

Inverting a for loop is usually as simple as reversing the index, so you change the outer loop to for (int i = 3; i >=1; i--), which gives an output of:

| /====\ |
|  /--\  |
+--------+
|   /\   |

You'll notice the final line prints to early so you should switch the \\TOP OR BOTTOM LINE block with the \\INTERIOR LINES block, which outputs:

| /====\ |
|  /--\  |
|   /\   |
+--------+

Now you just need to switch your back-slashes and forward-slashes. The resultant code prints a nice second half:

| \====/ |
|  \--/  |
|   \/   |
+--------+

The code being:

// 2nd Half of Diamond
for (int i = 3; i >= 1; i--) {
    //INTERIOR LINES
    {
        System.out.print("|");
        // Nested Loop Creates Spaces Left Side
        for (int j = 4; j > i; j--) {
            System.out.print(" ");
        }
        System.out.print("\\");
        // Nested Loop Creates Values Inside
        for (int j = 1; j < i; j++) {
            if (i % 2 == 0) {
                System.out.print("--");
            } else if (i == 1) {
                System.out.print("/");
            } else {
                System.out.print("==");
            }
        }
        System.out.print("/");
        // Nested Loop Creates Spaces Right Side
        for (int j = 4; j > i; j--) {
            System.out.print(" ");
        }
        System.out.print("|");
        System.out.print("\n");
    }

    //TOP OR BOTTOM LINE
    {
        if (i == 1) {
            System.out.print("+");
            for (int h = 1; h <= 8; h++) {
                System.out.print("-");
            }
            System.out.print("+" + "\n");
        }
    }
}
Linus
  • 894
  • 7
  • 13
0

Two nested for loops from -n to n and a couple of if else statements. The zero point is in the center of the rhombus.

Try it online!

public static void main(String[] args) {
    printDiamond(5);
}

static void printDiamond(int n) {
    System.out.println("n=" + n);
    // vertical axis
    for (int i = -n; i <= n; i++) {
        // horizontal axis
        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(" ");
        System.out.println(); // new line
    }
}

Output:

n=5
+----------+
|    /\    |
|   /--\   |
|  /====\  |
| /------\ |
|<========>|
| \------/ |
|  \====/  |
|   \--/   |
|    \/    |
+----------+

See also: How to print a diamond with a frame?