day03(循环)

1.for循环

1.1.for的格式

格式:
    for(1初始化语句;2条件判断语句;3条件控制语句){
    	4循环体语句;
    }
    
执行流程:
	1   2  4  3   2  4  3   2

1.2循环输出10次广告

package com.itheima01;

public class Demo01 {
    public static void main(String[] args) {
        //循环10次广告

        for(int i=0; i<10; i++){         // 0 1 2 3 4  5 6 7 8 9
            System.out.println("秋天不吃西瓜...");
        }
        

        for(int i=1; i<=10; i++){        // 1 2 3 4 5  6 7 8 9 10
            System.out.println("秋天吃桃...");
        }
        
    }
}

1.3循环15和51

package com.itheima01;

public class Demo02 {
    public static void main(String[] args) {

        //输出1-5的数字
        for(int i=1; i<=5; i++){
            System.out.println(i);
        }

        //输出5-1的数字
        for(int i=5; i>=1; i--){
            System.out.println(i);
        }

    }
}

1.4求1-5的和

package com.itheima01;

public class Demo03 {
    public static void main(String[] args) {

        //求1-5的和
        int sum = 0;

        for(int i=1; i<=5; i++){
            sum = sum+i;
        }
        
        System.out.println(sum);        
        /*
            sum = 0+1;         sum=1
            sum = 1+2;         sum=3
            sum = 3+3;         sum=6
            sum = 6+4;         sum=10
            sum = 10+5;        sum=15
         */
    }
}

1.5求数字的奇偶数

package com.itheima01;

public class Demo04 {
    public static void main(String[] args) {

        //循环开始就是0,循环结束10
        for(int i=0; i<=10; i++){
            //判断语句
            if(i%2==0){
                System.out.println(i+"是偶数");
            }else{
                System.out.println(i+"是奇数");
            }
        }
    }
}

2.while循环

2.1while的格式

格式:
    初始化语句;
    while(条件判断语句){
          循环体语句;
          条件控制语句;
    }

2.2输出1-5和5-1

package com.itheima01;

public class Demo05 {
    public static void main(String[] args) {

        //输出1-5
        int i = 1;
        while(i<=5){
            System.out.println(i);
            i++;
        }

        //输出5-1
        int j = 5;
        while(j>=1){
            System.out.println(j);
            j--;
        }

    }
}

2.3求1-100的和

package com.itheima01;

public class Demo06 {
    public static void main(String[] args) {
        //while循环求1-100的和
        int sum = 0;

        int i = 1;

        while(i<=100){
            sum += i;
            i++;
        }

        System.out.println("1到100的和是" + sum);

    }
}

2.4珠穆朗玛峰

package com.itheima01;

public class Demo07 {
    public static void main(String[] args) {
        //定义纸厚度
        double zhi = 0.0001;
        //定义山高度
        int shan = 8848;

        //定义变量记录折叠的次数
        int count = 0;

        //编写while循环
        while(zhi <= shan) {
            //每折一次纸让纸的厚度乘2
            zhi = zhi*2;
            //每折一次纸让次数加1
            count++;
        }
        
        //循环结束后, 最终打印次数
        System.out.println(count);

    }
}

3.do…while

3.1do…while的格式

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

3.2打印1-5

package com.itheima01;

public class Demo08 {
    public static void main(String[] args) {
        //打印1-5
        int i = 1;

        do{
            System.out.println(i);
            i++;
        }while (i<=5);

    }
}

4.三种循环区别

for:	
	在明确循环次数的时候建议使用

while:
	在不明确循环次数的时候建议使用
	在写死循环时候建议使用
        while(true){
        }
        
do..while:
	没有使用场景...

5.Debug代码调试

debug的功能就是查看代码的执行流程,看代码中的问题

用法:
	1.加断点,哪里不会点哪里
	2.右键选择debug运行
	3.点F8向下执行
	4.点击stop结束程序
	5.点断点,去掉断点

6.循环跳转语句

continue:
	在循环中,表示跳过某次循环,进入下一次循环
break:
	在循环中,表示终止循环,整个当前循环就结束了
	
注意事项:
	1.任何循环都可以使用,for   while   do.while..
	2.必须用在判断语句中
package com.itheima02;

public class Demo01 {
    public static void main(String[] args) {

        //输出1-100,跳过7的倍数
        for(int i=1; i<=100; i++){
            if(i%7==0){
                continue;
            }
            System.out.println(i);
        }
    }
}

7.循环嵌套

package com.itheima02;

public class Demo02 {
    public static void main(String[] args) {
        //一天有24个小时
        for(int i=0; i<24; i++){
            //一小时有60分钟
            for(int j=0; j<60; j++){
                System.out.println(i + "小时" + j+"分钟");
            }
        }

        /*
            i=0
                    j=0       0时0分
                    j=1       0时1分
                    j=2       0时2分
                    ...
                    j=59      0时59分
            i=1
                    j=0       1时0分
                    j=1       1时1分
                    j=2       1时2分
                    ...
                    j=59      1时59分
             i=23
                    j=0       23时0分
                    j=1       23时1分
                    j=2       23时2分
                    ...
                    j=59      23时59分
         */

    }
}
package com.itheima02;

public class Demo03 {
    public static void main(String[] args) {

        //循环4次代表4行
        for(int i=0; i<4; i++){
            //循环5次代表一行5颗星
            for(int j=0; j<5; j++) {
                System.out.print("*");
            }
            //换行
            System.out.println();
        }
        /*
             *****
             *****
             *****
             *****
         */

    }
}

8.Random随机数

1.导包
	import java.util.Random;

2.创建对象
	Random r = new Random();
	
3.生成随机数(0-9的随机数)
	int a = r.nextInt(10);
package com.itheima02;
//导包
import java.util.Random;

public class Demo05 {
    public static void main(String[] args) {

        //创建对象
        Random r = new Random();

        //生成0-9的随机数
        int a = r.nextInt(10);
        //打印
        System.out.println(a);

        //生成0-99的随机数
        int b = r.nextInt(100);
        //打印
        System.out.println(b);

        //思考,每次生成的都是0到?的数字,如果我想要1-10的随机数怎么办?
        int c = r.nextInt(10) + 1;
        
        //思考,如果想要有个11~20的随机数
        int d = r.nextInt(10) + 11;
        
        //思考规律,如果想要生成n~m的随机数
        //      r.nextInt(m-n+1) + n;
        
        //想要20-40的随机数
        //         0 ~ 20
        int e = r.nextInt(21) + 20;
    }
}

9.猜数字小游戏

package com.itheima02;

import java.util.Random;
import java.util.Scanner;

public class Demo06 {
    public static void main(String[] args) {
        /*
            程序自动生成一个1-100之间的数字,使用程序实现猜出这个数字是多少?
             根据不同情况给出相应的提示
                如果猜的数字比真实数字大,提示你猜的数据大了
                如果猜的数字比真实数字小,提示你猜的数据小了
                如果猜的数字与真实数字相等,提示恭喜你猜中了
         */

        //1.生成一个1-100的随机数
        Random r = new Random();
        int num = r.nextInt(100)+1;

        //循环
        while (true) {
            //2.键盘输入一个数字
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入一个1-100的整数:");
            int a = sc.nextInt();

            //3.比较大小
            if (a > num) {
                //3.1猜大了
                System.out.println("你猜大了~");
            } else if (a < num) {
                //3.2猜小了
                System.out.println("你猜小了");
            } else {
                //3.3猜中了
                System.out.println("恭喜猜对了!");
                //结束循环
                break;
            }
        }

    }
}
学习知识点, 学习思路和经验




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值