a triangle problem
I thought this would be easy. This creates a triangle and i want to invert it.
import java.util.*;
public class Triangle {
public static void main (String [ ] args) {
int x, y , z, ht;
Scanner kb = new Scanner(System.in);
System.out.print ("enter height");
ht = kb.nextInt( );
for (x=0; x<ht; x++)
{
for (y=ht; y>x; y--)
System.out.print(" ");
for (z=0;z<=x; z++)
System.out.print ("*");
}
}
}
OK, i understand the first for statement, x will increase until it reaches the height entered. I also understand the next 2 for statements. With this in mind I thought it would be simple to invert the triangle. Can anyone point me in the direction I need to go.
Thanks
will
[809 byte] By [
grjmmr1a] at [2007-11-26 20:02:24]

I can't really tell what you are trying to do with your code (Hint* Use [code]tags next time :) )
But what I would do is started a for loop with a variable equaling the height. While this variable is greater than zero, decrement it.
Inside of this loop, will contain another for loop. This for loop will contain a new variable starting at zero, and increments by one until it is less than the first for loop's variable.
Inside of the second for loop with have a simple print("*");
Message was edited by:
lethalwire
you need to add another statement
System.out.println("\n");
import java.util.*;
public class Triangle {
public static void main (String [ ] args) {
int x, y , z, ht;
Scanner kb = new Scanner(System.in);
System.out.print ("enter height");
ht = kb.nextInt( );
for (x=0; x<ht; x++)
{
//for (y=ht; y>x; y--)
//System.out.print(" ");
for (z=0;z<=x; z++)
System.out.print ("*");
System.out.print("\n");
}
}
}
Message was edited by:
fastmike