Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So I have been asked this question and I could only solve the top part of the code, I am stuck on the bottom part.

Write a Java program called EmptyDiamond.java that contains a method that takes an integer n and prints a empty rhombus on 2n ? 1 lines as shown below. Sample output where n = 3:

  1
 2 2
3   3
 2 2
  1

Here's my code so far:

public static void shape(int n) {
    //TOP PART
    for (int i = 1; i <= (n - 1); i++) {
        System.out.print(" ");
    }
    System.out.println(1);
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j <= (n - i); j++) {
            System.out.print(" ");
        }
        System.out.print(i);
        for (int j = 1; j <= 2 * i - n + 1; j++) {
            System.out.print(" ");
        }
        System.out.println(i);
    }

    //BOTTOM PART (The messed up part)
    for (int i = n + 1; i <= 2 * n - 2; i++) {
        for (int j = 1; j <= n - i; j++) {
            System.out.print(" ");
        }
        System.out.print(i);
        for (int j = 1; j <= n; j++) {
            System.out.print(" ");
        }
        System.out.print(i);
    }
    for (int i = 1; i <= (n - 1); i++) {
        System.out.print(" ");
    }
    System.out.println(1);
}
public static void main(String[] args) {
    shape(4);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
278 views
Welcome To Ask or Share your Answers For Others

1 Answer

Maybe a little bit late, but because the bottom part of your message is just the first part mirrored you can use a Stack to print the message in reverse order:

public static void main(String[] args) {
    int maxNumber = 3;
    Stack<String> message = new Stack<>();
    // upper part
    for (int row = 0; row < maxNumber; row++) {
        int prefix = maxNumber - (row + 1);
        int spaces = row >= 2 ? row * 2 - 1 : row;

        String line = getLine(row, prefix, spaces);
        System.out.println(line);
        if (row != maxNumber - 1)
            message.add(line);
    }
    // bottom part
    while (!message.isEmpty())
        System.out.println(message.pop());
}

public static String getLine(int row, int prefix, int spaces) {
    StringBuilder line = new StringBuilder("_".repeat(prefix));
    line.append(row + 1);
    if (row != 0) {
        line.append("_".repeat(spaces));
        line.append(row + 1);
    }
    return line.toString();
}

Output:

__1
_2_2
3___3
_2_2
__1

You can of course use any method you want to fill the stack (i.e. to generate the upper part of your message) like with the method this question suggessted. This upper part I describe contains the first line (inclusive) up to the middle line (exclusive).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...