Java Reference
In-Depth Information
for (int j = 1; j <= i; j++)
r = r + Ð[]Ñ;
r = r + Ð\nÑ;
244
245
Putting both loops together yields two nested loops:
String r = ÐÑ;
for (int i = 1; i <= width; i++)
{
// Make triangle row
for (int j = 1; j <= i; j++)
r = r + Ð[]Ñ;
r = r + Ð\nÑ;
}
return r;
Here is the complete program:
ch06/triangle1/Triangle.java
1 /**
2This class describes triangle objects that can be displayed
3as shapes like this:
4 []
5 [][]
6 [][][].
7*/
8 public class Triangle
9 {
10 /**
11Constructs a triangle.
12 @param aWidth the number of [] in the last row of the
triangle
13 */
14 public Triangle(int aWidth)
15 {
16 width = aWidth;
17 }
18
19 /**
20Computes a string representing the triangle.
21 @return a string consisting of [] and newline characters
Search WWH ::




Custom Search