Question

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.

Was it helpful?

Solution 2

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("*");

OTHER TIPS

java-11

By using String#repeat, introduced as part of Java-11, you can do it with a single loop.

public class Main {
    public static void main(String[] args) {
        int size = 9;
        int midRowNum = size / 2 + 1;
        for (int i = 1 - midRowNum; i < midRowNum; i++) {
            System.out.println(" ".repeat(Math.abs(i)) + "*".repeat((midRowNum - Math.abs(i)) * 2 - 1));
        }
    }
}

Output:

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

By increasing the amount of space by one character, you can also print a variant of the shape:

public class Main {
    public static void main(String[] args) {
        int size = 9;
        int midRowNum = size / 2 + 1;
        for (int i = 1 - midRowNum; i < midRowNum; i++) {
            System.out.println("  ".repeat(Math.abs(i)) + "* ".repeat((midRowNum - Math.abs(i)) * 2 - 1));
        }
    }
}

Output:

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

Using Math.abs will make it a lot easier:

import java.util.Scanner;

public class MakeDiamond {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("Let's Creat Diamonds");
            System.out.println("If number increases Diamonds gets bigger. " +
                    "Please input number lager than 1 : ");

            int user_input = sc.nextInt(); //gets user's input
            System.out.println("");

            int x = user_input;
            int front_space = -5;

            for (int i = 0; i < 2 * user_input + 1; i++) {
                for (int a = front_space; a < Math.abs(i - user_input); a++) {
                    System.out.print("    ");
                }

                if (i < user_input + 1) {
                    for (int b = 0; b < 2 * i + 1; b++) {
                        System.out.print("*  ");
                    }

                } else if (i > user_input) {
                    for (int c = 0; c < 2 * x - 1; c++) {
                        System.out.print("*  ");
                    }
                    x--;
                }
                System.out.print('\n');
            }

            System.out.println("\nRun Again? 1 = Run,  2 = Exit : ");

            int restart = sc.nextInt();
            System.out.println("");

            if (restart == 2) {
                System.out.println("Exit the Program.");
                System.exit(0);
                sc.close();
            }
        }
    }
}

You can simplify your code by using two nested for loops and one if else statement.

int n = 4;
for (int i = -n; i <= n; i++) {
    for (int j = -n; j <= n; j++)
        if (Math.abs(i) + Math.abs(j) <= n)
            System.out.print("*");
        else
            System.out.print(" ");
    System.out.println();
}

Output:

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

See also: Output an ASCII diamond shape using loops

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top