for loop
practice
public class Triangle {
public static void main(String[] args) {
for (int line = 1; line <= 4; line++) {
for (int stars = 1; stars <= expression ; stars++) {
System.out.print("*");
}
System.out.println();
}
}
}
continued on the next slide...
We want to produce the following output:
******* ***** *** *
Fill in the table below indicating how many stars appear on each line of output.
| Line | Stars |
|---|---|
1
|
7 |
2
|
5 |
3
|
3 |
4
|
1 |
continued on the next slide...
We need an expression for the number of stars on each line of this form:
multiplier * line + constant
for (int stars = 1; stars <= expression ; stars++) {
Your program should now produce the correct output. You can verify with
the Output
Comparison Tool.