Skip to content
Advertisement

Draw an ASCII diamond in a frame

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("=");
        }
    }
}

Advertisement

Answer

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:

+--------+
|   /   |
|  /--  |
| /==== |
|<------>|
| ====/ |
|  --/  |
|   /   |
+--------+
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement