samedi 23 mai 2020

How to do this type of Pattern in java but only using for loop and nesting

"""this is my code and I am trying to make a triangle pattern like this"""

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

and I don't know where is the problem in this code.

please fix my problem in this code.

public static void main(String[] args) {

    Scanner sc = new Scanner (System.in);

    int n = sc.nextInt();

    for ( int i = 1; i <= n; i++) {

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

}

}

Aucun commentaire:

Enregistrer un commentaire