Hi all, Im new to the site and to Java. Im having some trouble with my current assignment. I was suposed to make an isosceles triangle with asterisks based on a number entered by user. I got the first half correct but I can not figure out my loops for the second half. Do I have to start a new main to get it to descend? Here is what I have done so far. It compiles fine but will not run the descending half of pyramid. Im not sure it makes a difference but I use JCreator for IDE.

Thanks for any assistance.

 import java.util.*;

  public class homeWork6
  {
     public static void main(String[] args)
     { 
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter an interger from 1 to 50:");
        int number = keyboard.nextInt(); 
        System.out.println();

        if (number < 1)
        System.out.println("Invalid number: must be at least one.");
        else if (number > 50)
            System.out.println("Invalid number: cannot exceed 50.");
        else // valid input print triangle of asterisks
        { 
          // print first half
          int lineCount;
          int asteriskCount; 
          for (lineCount = 1; lineCount <= number; lineCount++)
          {
             for (asteriskCount = 1; asteriskCount <= lineCount;
                    asteriskCount ++)
             {
                  System.out.print("*");    

             }//end inner for loop
               System.out.println();

          }//end outer for loop

             for (lineCount = 1; lineCount >= number; lineCount --)
           {

            for (asteriskCount = number; asteriskCount >= lineCount;
             asteriskCount --)          

            System.out.print("*");

          }

                System.out.println();

         } //end else

       }//end main

  }//end class

Edited 3 Years Ago by pyTony: fixed formating

You need to come up with a formula for calculating the number of spaces to use at the beginning. There is an alternative way by using char arrays, but that is very difficult.

you could have an int which will say how many spaces to use and then while you are going through the loop and sticking down stars, you decrease the amount of spaces by one if the piramid gets fatter and then increase the spaces and decrease the amount of stars you put down when the piramid gets thinner.

make sense? ;)

try drawing the piramid thingy on paper and then look at what it is and then also look at what changes in the next line. put little blocks down as spaces.

No fancy stuff needed... your for statement is just screwed up for second half.


for (lineCount = number; lineCount >= 0; lineCount --)
{

for (asteriskCount = lineCount; asteriskCount >= 0;
asteriskCount --)

System.out.print("*");

}

System.out.println();

} //end else

You have to start at the condition you used going up and then use the start you used going up as the condition coming down. Just using -- won't cut it.

Just wanted to say thank you for all the help. I have learned a lot from this forum by just readiing other threads.

This article has been dead for over six months. Start a new discussion instead.