for嵌套 break continue 控制语句

for循环嵌套练习

1,打印:

*****

*****

*****

*****

外循环控制行,内循环控制列。

for(int x=0;x<4;x++) {  
  1.     for(int y=0;y<5;y++) {  
  2.         System.out.print("*");  
  3.     }  
  4.     System.out.println();//换行   
  5. }  

2,打印:

*****

****

***

**

*

//方法1:
int z = 5;
for(int x=1;x<=5;x++) {
	for(int y=1;y<=z;y++) {
		System.out.print("*");
	}
	System.out.println();
	z--;
}
//方法2:
int z=1;
for(int x=1;x<=5;x++) {
	for(int y=z;y<=5;y++) {
		System.out.print("*");
	}
	System.out.println();
	z++;
}
//方法3:
for(int x=1;x<=5;x++) {
	for(int y=x;y<=5;y++) {
		System.out.print("*");
	}
	System.out.println();
}

3,打印:

54321

5432

543

54

5

for(int x=1;x<=5;x++) {
	for(int y=5;y>=x;y--) {
		System.out.print(y);
	}
	System.out.println();
}

4,打印99乘法表

for(int x=1;x<=9;x++) {
	for(int y=1;y<=x;y++) {
		System.out.print(y+"*"+x+"="+y*x+"\t");
	}
	System.out.prtinln();
}

5,打印:

* * * * *

 * * * *

 * * *

  * *

   * 

 

for(int x=1;x<=5;x++)

 {  

                for(int y=1;y<x;y++)

                 {

                       System.out.print(" ");  

                 }  

               for(int z=x;z<=5;z++)

               {  

                        System.out.print("* ");  

                 }  

    System.out.println();  

}  

break的作用范围:要么是switch语句,要么是循环语句。

continue继续作用范围:循环结构。

记住:a:这俩个语句离开应用范围,存在是没有意义的。

            b:这俩个语句单独存在下面都不可以有语句,因为执行不到。

            c:continue语句是结束本次循环,继续下次循环。

            d:标号的出现,可以让这俩个语句作用于指定的范围。

 

1: 当break语句单独存在时,下面不要定义其他语句,因为执行不到。

break跳出所在的当前循环。

如果出现了循环嵌套,break想要跳出指定的循环,可以通过标号来完成。

2,continue继续

作用范围:循环结构。

作用:结束本次循环,继续下次循环。

如果continue单独存在时,下面不要有任何语句,因为执行不到。

continue也可以像break那样,定义标记继续执行指定的循环圈。

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值