This is the syntax highlighted version of Triangle.java.
/*************************************************************************
* Compilation: javac Triangle.java
* Execution: java Triangle N
*
* Prints out an N-by-N triangular pattern like the one below.
*
* % java Triangle
* * * * * * *
* . * * * * *
* . . * * * *
* . . . * * *
* . . . . * *
* . . . . . *
*
*************************************************************************/
public class Triangle {
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
// loop N times, one for each row
for (int i = 0; i < N; i++) {
// print j periods
for (int j = 0; j < i; j++)
System.out.print(". ");
// print N-i asterisks
for (int j = 0; j < N-i; j++)
System.out.print("* ");
// print a new line
System.out.println();
}
}
}
Last updated: Wed Feb 11 18:07:23 EST 2004
.
Copyright © 2004, Robert Sedgewick and Kevin Wayne.