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
1.2k views
in Technique[技术] by (71.8m points)

java - Drawing numeric diamond

I need to draw a numeric diamond, for example with a height of 9:

    1
   222
  33333
 4444444
555555555
 4444444
  33333
   222
    1

I wrote the code and I managed to get the same diamond, but with stars. I want it with these numbers. How can I do that? Here is what I have done so far:

for (int i = 1; i < 10; i += 2) {
    for (int j = 0; j < 9 - i / 2; j++)
        System.out.print(" ");
    for (int j = 0; j < i; j++)
        System.out.print("a");
    System.out.print("
");
}

for (int i = 7; i > 0; i -= 2) {
    for (int j = 0; j < 9 - i / 2; j++)
        System.out.print(" ");
    for (int j = 0; j < i; j++)
        System.out.print("b");
    System.out.print("
");
}
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Regarding your code:

  • System.out.print(" "); should be replaced by System.out.println().
  • You should a dynamic height instead of hard-coding 9.
  • It prints the correct pattern, only what is printed is wrong: instead of printing "a" and "b", you should print the index of the loop and see what you can get from there. This is @Tsung-Ting Kuo solution.

You can do it with fewer loops and a more understandable in my view. Consider the following algorithm:

  • For each row of the pattern (so the row goes from 0 to height excluded)
  • For each column of the pattern (so the column goes from 0 to height excluded)
  • We need to print a space when we are in the upper-right, upper-left, lower-right or lower-left of the diagram.
    • Upper-left: This is when the column is less than height/2-row-1
    • Lower-left: This is when the column is less than row-height/2
      • Factoring these two expressions in a single one, this is when the column is less than height/2 - min where min = Math.min(row+1, height-row).
    • Upper-right: This is when the column is greater than height/2+row+1
    • Lower-right: This is when the column is greater than height/2+height-row
      • Factoring these two expressions in a single one, this is when the column is greater than height/2 + min where min = Math.min(row+1, height-row).
  • Otherwise, we need to print Math.min(row+1, height-row).

Turning into code:

public static void main(String[] args) {
    int height = 9;
    for (int row = 0; row < height; row++) {
        for (int column = 0; column < height; column++) {
            int min = Math.min(row+1, height-row);
            if (column <= height / 2 - min || column >= height / 2 + min) {
                System.out.print(" ");
            } else {
                System.out.print(min);
            }
        }
        System.out.println();
    }
}

Sample output:

    1    
   222   
  33333  
 4444444 
555555555
 4444444 
  33333  
   222   
    1    

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

...