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

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

java - Empty diamond shape with numbers

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

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

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...