Monday, October 19, 2015

PyramidOfStars

public class PyramidOfStars
{
    public static void main(String[] args)
    {
        //prints number of spaces 
        int lines = 8;
       
        //first for loop determines how many lines are printed
        for(int i = 1; i <= 8; i++)
        {
            //second loop prints spaces
            for(int spaces = 1; spaces < lines; spaces++)
            {
                System.out.print(" ");
            }
            //decrements line number
            lines--;
            //third loop prints stars..start at one to print first star
            //and double i to get pyramid rather than triangle..eighth line
            //will be 15 stars and all lines will have an odd number of stars
                for(int stars = 1; stars < i * 2; stars++)
                {
                  System.out.print("*");
                }
            //creates a new line
            System.out.println();
       }
   }
}

I used a different strategy on this one. The loops determine spacing and printing stars rather than forcing formatting with specifiers.

No comments:

Post a Comment