class upsidedown {
public static void main(String args[]) {
int x, y;
for (y = 1; y <= 5; y++) {
for (x = 0; x < 5 - y; x++) {
System.out.print(' ');
}
for (x = (2 - y); x < (2 - y) + (2 * y - 1); x++) {
System.out.print('*');
}
System.out.print('\n');
}
}
}
So far my code prints out a regular, right side up triangle. How do I make it upside down?
解决方案
Very easily. Using your same logic, just reverse the order that you print your lines with.
public class UpsideDown {
public static void main(String args[]) {
int x, y;
for (y = 5; y >= 1; y--) { //reverse here
for (x = 0; x < 5 - y; x++)
System.out.print(' ');
for (x = (2 - y); x < (2 - y) + (2 * y - 1); x++)
System.out.print('*');
System.out.print('\n');
}
}
}