Help with this simple triangle program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • milk242
    New Member
    • Feb 2009
    • 25

    #1

    Help with this simple triangle program

    I am trying to figure out how to print a triangle in the shape of this depending on the user input. If they input 4 the max height would be 4.
    *
    **
    ***
    ****
    ***
    **
    *

    I have no idea how to print the triangle after it reaches its max height. So far my code is this. Please help!
    import java.util.Scann er;
    [code=java]
    public class Main
    {
    public static void main(String[] args)
    {
    int n, line, asterik;
    Scanner key = new Scanner(System. in);
    System.out.prin tln("Please enter the size of the triangle you want");
    n = key.nextInt();
    for(line = 1; line <= n+(n-1); line++)
    {
    if(line <= n)
    {
    for(asterik = 1; asterik <= line; asterik++)
    {
    System.out.prin t("*");
    }
    }
    else
    {

    for(asterik = n; asterik <= line && (asterik != 0); asterik--)
    {
    System.out.prin t("*");
    }
    }
    System.out.prin tln(" ");
    }
    }
    }[/code]
    Last edited by Nepomuk; Feb 26 '09, 10:43 PM. Reason: Please use [CODE] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You are trying to squeeze all your logic and control flow in one big loop that handles 2*n-1 lines. You have to test whether or not you're past half way. Why not implement two simple loops, one after the other, no nesting; the first loop handles the first n lines and the second one handles the last n-1 lines.

    For convenience you should define a little method that prints i characters on a line; something like this:

    Code:
    private static void printLine(int i, char c) {
       for (int p= 0; p < i; p++)
          System.out.print(c);
       System.out.println();
    }
    In your two loops I described above you can simply call that little method and all those auxiliary loops that only take care of the printing are gone from your two loops.

    kind regards,

    Jos

    Comment

    • milk242
      New Member
      • Feb 2009
      • 25

      #3
      Thanks Jos for the help that makes sense, but I was also wondering if its possible to do this task with only two for loops that are nested. I'm working out of a java book and it gives me a hint on using two for loops, one nested in another. I'm thinking its not possible because, how would you start the downward decrease of asterisk with only two loops?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by milk242
        Thanks Jos for the help that makes sense, but I was also wondering if its possible to do this task with only two for loops that are nested. I'm working out of a java book and it gives me a hint on using two for loops, one nested in another. I'm thinking its not possible because, how would you start the downward decrease of asterisk with only two loops?
        I'd do it like this:

        Code:
        for (int i= -3; i <= 3; i++) {
           int nofStars= 4-Math.abs(i);
           // print 'nofStars' on a line ...
        }
        ... but I don't find it very intuitive.

        kind regards,

        Jos

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Would have used 2 for loops like
          Code:
          for(i = 1; i <=n; i++){
            //println i stars
          }
          for(i = n-1; i >0; i--){
            //println i stars
          }
          First loop increments and second loop decrements (i--).

          Comment

          Working...