-2

Can someone help me complete this numeric diamond? I have the right side of the diamond printing out, but I'm having trouble printing out the left side of it. If anybody can help I would really appreciate it. I made some changes to my code. I now need my code to print one column in the middle of the diamond instead of two.

public class NumericDiamond {
    public static void main(String[] args) {
       /*
                   1         1
                4  3  4      2
             4  4  5  7  4   3
                5  3  5      4
                   4         5
        */
        int noOfColumns = 1;
        int noOfSpaces = 3;
        int start = 0;
        for (int i = 1; i <= 5; i++) {
            for (int j = noOfSpaces; j >= 1; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= noOfColumns; j++) {
                System.out.print(noOfColumns);
            }
            if (i < 5) {
                start = i;
            } else {
                start = 8 - i;
            }

            System.out.print(start + " ");
            start--;

            System.out.println();
            if (i < 3) {
                noOfColumns = noOfColumns + 2;
                noOfSpaces = noOfSpaces - 1;
            } else {
                noOfColumns = noOfColumns - 2;
                noOfSpaces = noOfSpaces + 1;
            }
        }
    }
}
CelticsGrl
  • 19
  • 3

2 Answers2

1

When you write something out to the screen, think in rows. In the first and last row, you print 1 random number. In the second and fourth row, you print 3 random numbers. In the middle row, you print 5 random numbers. You can use tab or spaces to place the numbers to their positions.

Random rnd = new Random();
for(int i = 0; i < 5; i++) {
    if(0 == i || 4 == i) System.out.println("\t\t" + (rnd.nextInt(5)+1) + "\t\t\t" + (i+1));
    if(1 == i || 3 == i) System.out.println("\t" + (rnd.nextInt(5)+1) + "\t" + (rnd.nextInt(5)+1) + "\t" + (rnd.nextInt(5)+1) + "\t\t" + (i+1));
    if(2 == i) System.out.println((rnd.nextInt(5)+1) + "\t" + (rnd.nextInt(5)+1) + "\t" + (rnd.nextInt(5)+1) + "\t" + (rnd.nextInt(5)+1) + "\t" + (rnd.nextInt(5)+1) + "\t" + (i+1));
}

Something like this.

Community
  • 1
  • 1
1

To print a diamond you can use two nested for loops (or streams) over rows and columns from -n to n. The diamond shape is obtained when n > iAbs + jAbs. The value of a cell depends on its coordinates i and j, or it can be some kind of constant or random value:

int n = 5;
for (int i = -n; i <= n; i++) {
    // absolute value of 'i'
    int iAbs = Math.abs(i);
    for (int j = -n; j <= n; j++) {
        // absolute value of 'j'
        int jAbs = Math.abs(j);
        // diamond shape (cell value = iAbs + jAbs)
        if (iAbs + jAbs > n) {
            System.out.print("  ");
        } else {
            System.out.print(" " + (iAbs + jAbs));
        }
    }
    System.out.println("  i=" + iAbs);
}

Output:

           5            i=5
         5 4 5          i=4
       5 4 3 4 5        i=3
     5 4 3 2 3 4 5      i=2
   5 4 3 2 1 2 3 4 5    i=1
 5 4 3 2 1 0 1 2 3 4 5  i=0
   5 4 3 2 1 2 3 4 5    i=1
     5 4 3 2 3 4 5      i=2
       5 4 3 4 5        i=3
         5 4 5          i=4
           5            i=5

Similarly, you can use two nested streams:

int n = 5;
IntStream.rangeClosed(-n, n)
        // absolute value of 'i'
        .map(Math::abs)
        .peek(i -> IntStream.rangeClosed(-n, n)
                // absolute value of 'j'
                .map(Math::abs)
                // diamond shape (cell value = n - i - j)
                .mapToObj(j -> i + j > n ? "  " : " " + (n - i - j))
                .forEach(System.out::print))
        .mapToObj(i -> "  i=" + i)
        .forEach(System.out::println);

Output:

           0            i=5
         0 1 0          i=4
       0 1 2 1 0        i=3
     0 1 2 3 2 1 0      i=2
   0 1 2 3 4 3 2 1 0    i=1
 0 1 2 3 4 5 4 3 2 1 0  i=0
   0 1 2 3 4 3 2 1 0    i=1
     0 1 2 3 2 1 0      i=2
       0 1 2 1 0        i=3
         0 1 0          i=4
           0            i=5

See also:
Drawing numeric diamond
Diamond with nested for loops