tags:

views:

424

answers:

3

I'm trying to print a sideways triangle in java. If the user enters 5, the output should be:

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

If the user enters 6, the output should be:

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

I've gotten it to work for the case when the user enters 5, 3, or 1 but my code seems to work for those three cases only. I was wondering if anyone could help me get my code working for more cases. Here it is:

public void printArrow( int n )
{ int asterisks = 1;
   for ( int i = 0; i <= n/2; i++ )
   {
       for ( int j = i; j < asterisks; j++ )
       {

         System.out.print( "*" );

        }
        asterisks += 3;
        System.out.println();
    }


    asterisks = asterisks / 2 - 2;
    for ( int i = 0;  i < n/2; i++ )
    {
        for ( int k = i; k < asterisks; k++ )
        {
            System.out.print( "*" );

        }
        if ( i == 1 )
        {
            System.out.print( "*" );
        }

        asterisks -= 2;
        System.out.println();
    }
}
A: 

Alright Will I'll bite

So the goal is to print out a triangle of stars. Well we are going to need a loop of some kind, probably with another internal loop. And we know that it's going to be symmetric as it's a triangle.

so I'd start with printing the fist half:

function triangle( input )
    i <- 1
    while i < input do
        for j from 1 to i do
            print "*"
        end for
        i <- i + 2
        print "\n"
    end while

After that we'd need to deal with the second half of the triangle which, because we have already walked i up to the input value means we can just walk it back down.

    if i > input then i <- i - 2

    while i > 0 do
        for j from 1 to i do
            print "*"
        end for
        i <- i - 2
        print "\n"
    end while
end function triangle

the little trick in it that almost caught me is the subtraction of two before the second while, if you don't do this you'll get the wrong answer. I'll leave figuring out why up to you. If there is confusing in the pseudocode notation please ask.

Mimisbrunnr
Thanks for the reply. I'll check it out tomorrow in more detail. It's late here and I have to get up early tomorrow. Thanks again!
Will
A: 
double middle = ((double) lines) / 2;
int asterisks = 1;
for (int i = 1; i <= lines; i ++){
    for (int k = 0; k < asterisks; k ++) {
        System.out.print("*");
    }

    if (i < middle) {
        asterisks += 2;
    } else if (i > middle){
        asterisks -= 2;
    }
    System.out.println();
}

Explaining:

  • lines is the input number (3,4,5,6,7, etc)
  • get the middle row as a double. I.e. for odd numbers it will be x.5
  • the loop is for as many lines as the input is
  • on each line print as many asterisk as there are in the asterisks variable
  • on each iteration either increase the number of asterisk by 2, if the line is before the middle, or decrease it, if after. This means that if it is equal, nothing happens - i.e. the same row has the same number of asterisk. And it can't be equal for odd numbers.
Bozho
+1  A: 

It's much easier to solve this using recursion:

static String triangle(int n, String s) {
    return
        n == 0 ? "" :
        n == 1 ? s  :
        s
          + 
            triangle(n - 2, "**" + s)
          +
        s
    ;
}

public static void main(String args[]) {
    System.out.println(triangle(6, "*\n"));
}

The structure of the triangle is self-evident:

  • n == 0? No line!
  • n == 1? One line!
  • Otherwise? Two lines sandwiching n - 2 lines! (which are longer!)
polygenelubricants