Skip to content
Advertisement

Making an ASCII rhombus with loops

I got a problem to create a rhombus, my code here:

package random;

public class asd {
    public static void main(String args[]) {
        for (int j = 1; j <= 4; j++) {
            for (int kong = 4 - j; kong >= 1; kong--) {
                System.out.print(" ");
            }
            for (int xing = 1; xing <= 2 * j - 1; xing++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (int a = 1; a <= 3; a++) {
            for (int b = 1; b <= a; b++) {
                System.out.print(" ");
            }
            for (int c = 5; c >= 1; c -= 2) { // <==== here
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

However, the output is:

   *
  ***
 *****
*******
 ***
  ***
   ***

I think the problem is in the code which I highlighted.

Advertisement

Answer

You are right in indicating the possible problematic line. Surprised that you did it right in first half:

for (int c = 5; c >= 2 * a - 1; c -= 1) { // <==== here
    System.out.print("*");
Advertisement