java内外循环打印阶梯*号

一.顺阶梯*打印

  先上结果图  

  思路:该结果图的基本组成单位是一个*号,采用内外for循环来实现,代码如下:

 1 class dayin 
 2 {
 3     public static void main(String[] args)
 4     {
 5         for(int x=4;x>=0;x--)
 6         {
 7             for(int y=5;(y-x)>0;y--)
 8             {
 9                 System.out.print("*");
10             }
11             System.out.println();
12         }
13     }
14 }

二.倒阶梯打印

  直接上代码1:

 1 class dayin 
 2 {
 3     public static void main(String[] args)
 4     {
 5         for(int x=0;x<5;x++)
 6         {
 7             for(int y=5;(y-x)>0;y--)
 8             {
 9                 System.out.print("*");
10             }
11             System.out.println();
12         }
13     }
14 }

  代码2:

 1 class dayin 
 2 {
 3     public static void main(String[] args)
 4     {
 5         for(int x=0;x<5;x++)
 6         {
 7             for(int y=x;y<5;y++)
 8             {
 9                 System.out.print("*");
10             }
11             System.out.println();
12         }
13     }
14 }

  

  通过在外循环里加入System.out.println(); 来实现跳行。

总结:会发现,外循环控制的是行数,内循环控制的是列数(个数),那么自然而然地,外循环只需让其循环5次即可,

关键的一点是在内循环的循环条件,调整行变量和列变量的关系以达目的,利用好便会游刃有余。

转载于:https://www.cnblogs.com/gaigaichen/p/8258632.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值