mercredi 5 février 2020

How can I flip this pattern countdown pattern?

I'm trying to create a countdown patten that asks the user for a number and prints a pyramid counting down to that number line by line. So if the user enters 5 the output is supposed to look like this :

                   1
                 2 1
               3 2 1
             4 3 2 1
           5 4 3 2 1

So far I know that to count up my code should look like this: Scanner scnr = new Scanner(System.in);

    int n;
    int j;
    int temp = 0;
    System.out.println("Please enter a number 1...9 :");

    n = scnr.nextInt();


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

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

        System.out.println("");

    }

Which would print out this:

         1 
       1 2 
     1 2 3 
   1 2 3 4 
 1 2 3 4 5 

But how can I have it count to 5 on the left side?

Aucun commentaire:

Enregistrer un commentaire