Output an ASCII diamond shape using loops
Asked Answered
E

3

3

I am trying to write a program in Java that captures an integer from the user (assume data is valid) and then outputs a diamond shape depending on the size of the integer, i.e. User enters 5, output would be:

--*--
-*-*-
*---*
-*-*-
--*--

So far I have:

if (sqr < 0) {
    // Negative
    System.out.print("#Sides of square must be positive");
}
if (sqr % 2 == 0) {
    // Even
    System.out.print("#Size (" + sqr + ") invalid must be odd");
} else {
    // Odd
    h = (sqr - 1) / 2; // Calculates the halfway point of the square
    // System.out.println();
    for (j = 0; j < sqr; j++) {
        for (i = 0; i < sqr; i++) {
            if (i != h) {
                System.out.print(x);
            } else {
                System.out.print(y);
            }
        }
        System.out.println();
    }
}

Which just outputs:

--*--
--*--
--*--
--*--
--*--

I was thinking about decreasing the value of h but that would only produce the left hand side of the diamond.

Everglades answered 25/10, 2011 at 10:8 Comment(0)
O
3
void Draw(int sqr) {
    int half = sqr / 2;
    for (int row = 0; row < sqr; row++) {
        for (int column = 0; column < sqr; column++) {
            if ((column == Math.abs(row - half))
                    || (column == (row + half))
                    || (column == (sqr - row + half - 1))) {
                System.out.print("*");
            } else {
                System.out.print("_");
            }
        }
        System.out.println();
    }
}

Ok, now this is the code, but as I saw S.L. Barth's comment I just realised this is a homework. So I strongly encourage you to understand what is written in this code before using it as final. Feel free to ask any questions!

Orphism answered 25/10, 2011 at 10:35 Comment(0)
A
2

Take a look at your condition:

if (i != h)

This only looks at the column number i and the midway point h. You need a condition that looks at the column number and the row number. More precisely, you need a condition that looks at the column number, the row number, and the distance of the column number from the midway point.
Since this is a homework question, I leave determining the precise formula to you, but I'm willing to drop some more hints if you need them. Good luck!

Ammonal answered 25/10, 2011 at 10:23 Comment(0)
L
1

You can use two nested for loops from -h to h, where h is half a diamond. The edge of a diamond is obtained when:

Math.abs(i) + Math.abs(j) == h

If user input n=5, then h=2, and a diamond looks like this:

n=5, h=2
--*--
-*-*-
*---*
-*-*-
--*--

Try it online!

// user input
int n = 9;
// half a diamond
int h = n / 2;
// output a diamond shape
System.out.println("n=" + n + ", h=" + h);
for (int i = -h; i <= h; i++) {
    for (int j = -h; j <= h; j++) {
        if (Math.abs(i) + Math.abs(j) == h) {
            System.out.print("*");
        } else {
            System.out.print("-");
        }
    }
    System.out.println();
}

Output:

n=9, h=4
----*----
---*-*---
--*---*--
-*-----*-
*-------*
-*-----*-
--*---*--
---*-*---
----*----
Lacee answered 27/3, 2021 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.