Working with Loops:
The Triangle class.
Here is a program that solves one of the practice problems
for the final exam.
// Triangle.java: print a triangle
public class Triangle {
public static void main (String[] args) {
int n = 10;
chars(1, '*'); chars(1, '\n');
for (int line = 2; line < n; line ++) {
chars(1, '*'); chars(line-2, ' ');
chars(1, '*'); chars(1, '\n');
}
chars(n, '*'); chars(1, '\n');
}
public static void chars(int n, char ch) {
for(int i = 0; i < n; i++)
System.out.print(ch);
}
}
The output.
*
**
* *
* *
* *
* *
* *
* *
* *
**********