mercredi 9 novembre 2016

How do I make an isosceles triangle upside down using nested for loops and extended if statements?

//Example 1: OUTPUT

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

//Example 1: Code

 for(int lines = 1; lines <= 4; lines++)
 {
     if (lines == 1)    
        System.out.printf("%4s", " ");`
    else if (lines == 2)
        System.out.printf("%3s", " ");
    else if (lines == 3)
        System.out.printf("%2s", " ");
    else if (lines == 4)
        System.out.printf("%1s", " ");

    for(int stars = 1; stars <= 2 * lines - 1; stars++)
        System.out.print ('*');

    System.out.println();
 }

Refer to the previous example and write a nested for loop structure that will print the following:

Example 2: OUTPUT

          *****
           ***
            *

Example 2: Code (my attempt)

    for (int lines = 3; lines >= 1; lines--)
    {
        if (lines == 3) 
            System.out.printf("%1s", " ");
        else if (lines == 2)
            System.out.printf("%2s", " ");
        else if (lines == 1)
            System.out.printf("%3s", " ");

        for (int stars = 3; stars <=  3* lines - 1; stars++);
        {
            System.out.print('*');
        }

        //System.out.println();
    }

Aucun commentaire:

Enregistrer un commentaire