12、while语句&do while语句&while和for间的区别、循环嵌套等

while语句格式:
初始化语句;
  while(判断条件语句){
     循环体语句;
     控制条件语句;
  }
for语句格式:
  for(初始化语句;判断条件语句;控制条件语句){
    循环体语句;
  }
练习:用while循环实现 求出1-100之和
public class WhileDemo1 {
    public static void main(String[] args) {        
        
        int sum2=0;
        int y=1;
        while(y<=100){
            sum2+=y;
            y++;
        }
        System.out.println("sum2="+sum2);
    }
}

运行结果:

sum2=5050

练习:用for循环实现 求出1-100之和

public class WhileDemo1 {
    public static void main(String[] args) {
        //for语句版本
        int sum=0;
        for(int x=1;x<=100;x++){
            sum+=x;
        }
        System.out.println("sum="+sum);
        System.out.println("---------");
    }
}

运行结果:

sum=5050
---------

while语句和for语句的区别:

如果是一个范围,用for循环,如果是不明确要做多少次,用while循环较为合适。

练习:用while循环实现 统计水仙花数有几个?

public class WhileDemo2 {
    public static void main(String[] args) {
        int count2=0;
        int y=100;
        while(y<=999){
            int ge=y%10;
            int shi=y/10%10;
            int bai=y/10/10%10;

            if(ge*ge*ge+shi*shi*shi+bai*bai*bai == y){
                count2++;
            }
            y++;
        }
        System.out.println("count2:"+count2);
    }
}

练习:用while循环实现 统计水仙花数有几个?

public class WhileDemo2 {
    public static void main(String[] args) {
        int count2=0;
        int y=100;
        while(y<=999){
            int ge=y%10;
            int shi=y/10%10;
            int bai=y/10/10%10;

            if(ge*ge*ge+shi*shi*shi+bai*bai*bai == y){
                count2++;
            }
            y++;
        }
        System.out.println("count2:"+count2);
    }
}

运行结果:

count2:4

练习:用for循环实现 统计水仙花数有几个?

public class WhileDemo2 {
    public static void main(String[] args) {
        int count=0;
        for(int x=100;x<=999;x++){
            int ge=x%10;
            int shi=x/10%10;
            int bai=x/10/10%10;

            if(ge*ge*ge+shi*shi*shi+bai*bai*bai == x){
                count++;
            }
        }
        System.out.println("count:"+count);
        System.out.println("------------");
    }
}

运行结果:

count:4
------------
do...while的格式:
初始化语句;
do{
    循环体语句;
    控制条件语句;
}while(判断条件语句);

do while语句练习1:输出10次Helloworld。

public class DoWhileDemo {
    public static void main(String[] args) {
        //使用do...while语句输出10次Helloworld.
        int count=0;
        do{
            System.out.println("helloworld");
            count++;
        }while(count<10);

    }
}

运行结果(略)

do while语句练习2:求1-100的和。

public class DoWhileDemo {
    public static void main(String[] args) {
        //求1-100和
        int sum=0;
        int x=1;
        do{
            sum+=x;
            x++;
        }while(x<=100);
        System.out.println(sum);
    }
}

运行结果:

5050

循环嵌套使用

练习:请输出一个4行5列的星星图案(*)

public class ForForDemo {
    public static void main(String[] args) {
        for(int i=1;i<=4;i++){
            for(int j=1;j<=5;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }

}

运行结果:

*****
*****
*****
*****

练习:请输出下列的形郑文

*
**
***
****
*****
public class ForForDemo2 {
    public static void main(String[] args) {
        for(int i=1;i<=5;i++){
            for(int j=1;j<=i;j++){
                System.out.print("*");
            }
            System.out.println();

        }
    }
}

运行结果:

*
**
***
****
*****

练习:输出九九乘法表。

首先我们写出九九乘法表:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
...
1*9=9 2*9=18 3*9=27 ... 9*9=81
'\t'表示制表符,tab键的位置
public class ForForDemo3 {
    public static void main(String[] args) {
        for(int x=1;x<=9;x++){
            for(int y=1;y<=x;y++){
                System.out.print(y+"*"+x+"="+y*x+"\t");
            }
            System.out.println();
        }
    }

运行结果:

1*1=1	
1*2=2	2*2=4	
1*3=3	2*3=6	3*3=9	
1*4=4	2*4=8	3*4=12	4*4=16	
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值