nest 'for' loop.
7344 ワード
1 /*
2 nest for loop demo.
3 Note that,'upside' triangle controls 'inner condition'.
4 */
5 import kju.print.Print;
6 public class Pyramid {
7 public static void main(String[] args) {
8 upsideNumbers(5);
9 Print.println("------------------");
10 multiplicationTable(3);
11 Print.println("------------------");
12 pyramid(5);
13 }
14 /*shape begins:
15 ----*
16 ---* *
17 --* * *
18 -* * * *
19 shape ends:*/
20 static void pyramid(int height){
21 for(int i = 0; i < height; i++) { //all rows.
22 for(int j = i; j < height - 1; j++) //vitual for '-'.
23 Print.print(" ");
24 for(int j = 0; j <= i; j++)
25 Print.print("* ");
26 Print.println();
27 } //for(i)
28 }
29
30 /*shape begins:
31 1
32 12
33 123
34 1234
35 12345
36 shape ends:*/
37 static void upsideNumbers(int height) {
38 for (int i = 1; i <= height; i++) {
39 for (int j = 1; j <= i ; j++)
40 Print.print(j);
41 Print.println();
42 }//for(i).
43 }
44
45 /*shape begins:
46 1*1=1
47 1*2=2 2*2=4
48 1*3=3 2*3=6 3*3=9
49 shape ends:*/
50 static void multiplicationTable(int height){
51 for (int i = 1; i <= height; i++) {
52 for (int j = 1; j <= i; j++)
53 Print.print(j + "*" + i + "=" + (j * i) + "\t");
54 Print.println();
55 }//for(i)
56 }
57 }