Créez un motif en étoile en Java.


Je travaille sur un programme de modèle d'étoile simple en Java. J'ai le code en cours d'exécution mais il ne fait pas ce qu'il est censé. Mon code est:

public class q3 {

    public static void main(String[] args) {

        for (int i = 10; i >= 1; i--){
            for (int j = i; j >= 1; j--){
                System.out.print("*");              
            }           
            System.out.println("");
        }
    }
}

Sortie:

 $$$$$$$$$$
 $$$$$$$$$
 $$$$$$$$
 $$$$$$$
 $$$$$$
 $$$$$
 $$$$
 $$$
 $$
 $

Ce que je veux, c'est quelque chose comme ci-dessous:

         $
        $$
       $$$
      $$$$
     $$$$$
 ..........
 $$$$$$$$$$

Quelqu'un peut-il m'aider à comprendre comment j'obtiendrais le modèle ci-dessus. Merci.

Author: user3002108, 2017-03-06

1 answers

, Essayez ceci :

public static void main(String[] args) {

    final int length = 10;

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

        //Print spaces first
        for (int j = length - 1; j > i; j--) {
            System.out.print(" ");
        }

        //Then print "*"
        for (int j = 1; j <= i; j++) {
            System.out.print("*");
        }

        System.out.println();
    }
}

La sortie est pour length = 10:

       *
      **
     ***
    ****
   *****
  ******
 *******
********
 0
Author: Mouad EL Fakir, 2017-03-05 22:27:23