0

I am trying to figure out why my diamond is not completely forming at the bottom. When the user inputs any odd number bigger than three the diamond does not form correctly at the bottom (it only prints one line after the largest middle line). It looks like this (I tried five rows for this example):

    * 
   * * 
  * * * 
   * *

I believe there is something wrong with the for loops that are supposed to create the bottom of the diamond. The code for that is:

//bottom of the diamond
for (j = (in + 1) / 2; j < in; j++) {
    // similar to the first for statement instead
    // when j equals the user's input plus one
    // divided by two it will then go to the next
    // for loop (the diamond starts to shrink)
    for (i = 1; i < j; i++) {
        //when i equals one, and j is greater than one
        System.out.print(" "); //prints space
    }
    //similar to the for loop that prints the asterisk
    // to the left this for loop goes through when
    // l equals zero, and this time is less than
    // the user's input minus j
    for (l = 0; l < in - j; j++) {
        System.out.print(" *"); //prints the bottom asterisks
    }
    System.out.println(""); //starts another blank new line
}
// just like the top section it keeps looping
// until the requirements are no longer met

What am I doing wrong?

Community
  • 1
  • 1
Fyree
  • 107
  • 1
  • 1
  • 12

1 Answers1

0

Though Lashane pointed out the immediate problem in his comment, I noticed your logic is more complex than it needs to be.

So, in answer to your question

What am I doing wrong?

allow me to offer a less confusing implementation which uses identical loop body code for the top and bottom of the diamond.

int in = 5;
for (int j = 1; j <= in; j++) {
  for (int i = 0; i < in - j; i++) {
    System.out.print(" ");
  }
  for (int i = 0; i < j; i++) {
    System.out.print(" *");
  }
  System.out.println("");
}
for (int j = in - 1; j >= 1; j--) {
  for (int i = 0; i < in - j; i++) {
    System.out.print(" ");
  }
  for (int i = 0; i < j; i++) {
    System.out.print(" *");
  }
  System.out.println("");
}

Now the only difference between the two for loops is that the first counts up from 1 to in and the second counts back down from in - 1 to 1.

gknicker
  • 5,509
  • 2
  • 25
  • 41