More efficient way to code this?

Please refer to my code:

I defined multiple conditions based on the row numbers. If you were to do it, how would you reduce the size of the code? Is this even possible?
Request you to edit it or make a new one if thats okay.

@sinuscoupon0s_4659768f9d9b587e To reduce the size of this code Use the below approach :
Divide the given pattern in two parts from middle .Now do the work of these parts separately and print the required pattern accordingly.
Short Info about the variables:-

1. nsp (number of spaces)-> Number of spaces in very First Line of the pattern.
2. nst (number of stars)-> Number of stars in very first line of the pattern.
3. csp (counter of spaces)-> counter of spaces that will print the required number of spaces and will be initialized with 1 and incremented upto nsp.
4. cst (counter of stars)->  counter of spaces that will print the required number of stars and will be initialized with 1 and incremented upto nst.
5. rows -> It will be initialized with 1 and will go upto the total number of rows in the pattern.
  • Given pattern is seen as first we need to work for spaces and then for the numbers in each row.
  • Numbers are first increasing till the mid element and then decreasing till end of the row.
  • Thus, do the work of first spaces and then for numbers .
  • And then change variables accordingly for next iterations.

Always think of first row and then work for the next iteration and repeat the work total number of row times. That’s how you can solve any problem related to pattern.
Code :

import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner scn = new Scanner(System.in);

        int n = scn.nextInt();
        PattenG(n);
    }

    public static void PattenG(int n) {

        int nsp = n / 2 - 1;
        int rows = 1;
        int nst = n / 2;

        while (rows <= n) {

            if (rows <= n / 2 + 1) {

                if (rows <= n / 2) {
                    System.out.print("*");

                    int csp = 1;
                    while (csp <= nsp) {
                        System.out.print(" ");
                        csp++;
                    }
                } else {
                    int cst = 1;
                    while (cst <= nst) {
                        System.out.print("*");
                        cst++;
                    }
                }

                if (rows == 1 || rows == n / 2 + 1) {

                    int cst = 0;
                    while (cst <= nst) {
                        System.out.print("*");
                        cst++;
                    }
                } else {
                    System.out.print("*");
                }
            } else {

                if (rows < n) {
                    int csp = 0;
                    while (csp <= nsp) {
                        System.out.print(" ");
                        csp++;
                    }
                } else {
                    int cst = 1;
                    while (cst <= nst) {
                        System.out.print("*");
                        cst++;
                    }
                }

                System.out.print("*");

                int csp = 1;
                while (csp <= nsp) {
                    System.out.print(" ");
                    csp++;
                }

                System.out.print("*");

            }
            System.out.println();
            rows++;
        }
    }
}

Also, submitting the code is giving me this error:

@sinuscoupon0s_4659768f9d9b587e The code I sent you is not giving any error it is passing all testcases.(Online IDE or Assignment Question)

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.