nest 'for' loop.

 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 }

 

转载于:https://www.cnblogs.com/listened/p/3580193.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值