Search code examples
javanested-loops

Draw upside down asterisk triangle


OUTPUT

          *****
           ***
            *

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 = 1; stars <=  2* lines - 1; stars++);
        {
            System.out.print('*');
        }

        System.out.println();
    }

and I get this:

     *
       *
         *

I cannot find the problem, the code should run and work


Solution

  •      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 = 1; stars <=  2* lines - 1; stars++)
                System.out.print('*');
    
             System.out.println();
          }
    

    I HAD A ";" AFTER MY SECOND FOR LOOP IN THE ORIGINAL WHICH CAUSED IT TO ONLY RUN ONCE AND NOT BE A LOOP!