Java复习笔记入门阶段04:程序流程控制

目录

1.流程控制语句

2.分支结构

2.1 if分支(适合区间匹配)

2.2 switch分支(适合值匹配)

2.3执行流程:

3.循环结构

3.1 for循环

3.2 while循环

3.3 do while循环

3.4总结

4.死循环、循环嵌套

4.1死循环

4.2循环嵌套

5.跳转控制语句

6.随机数Random类


1.流程控制语句

控制程序的执行流程;

  • 顺序结构

    • 默认

  • 分支结构

    • if

    • switch

  • 循环结构

    • for

    • while

    • do while

2.分支结构

2.1 if分支(适合区间匹配)

三种格式:

public class IfDemo {
    public static void main(String[] args) {
        /**
         * If判断
         */
        //心跳60-100正常,否则要检查
        int heartBeat=30;
        if (heartBeat<60 || heartBeat>100){
            System.out.println("心跳数据是:"+heartBeat+"需要检查");
        }

        //转账
        double money=100;
        if (money<=100){
            System.out.println("转账成功");
        }else {
            System.out.println("余额不足");
        }

        //打分 >90 A+;>=80 A;>=70 B;>=60 C;<60 D
        int score = 98;
        if (score>90){
            System.out.println("成绩为:A+");
        }else if (score>=80){
            System.out.println("成绩为:A");
        }else if (score>=70){
            System.out.println("成绩为:B");
        }else if (score>=60){
            System.out.println("成绩为:C");
        }else {
            System.out.println("成绩为:D");
        }
    }
}

2.2 switch分支(适合值匹配)

  • switch注意事项:

  • 表达式类型只能是byte、short、int、char,

    • JDK5开始支持枚举,

    • JDK7开始支持String;

    • 不支持double、float、long;

  • case值不能重复,只能是字面量,不能是变量;

  • 不能忘记写break,会出现穿透现象

    • 穿透现象:没有遇到break,执行下一case,直到遇到break;

    • 可以解决代码冗余,多个条件有相同结果可以共用

    switch(weekDay){
        case "周一":
        case "周二":
        case "周三":
            System.out.println("打牌");
            break;
        default:
            System.out.println("沉迷打牌,不可自拔(^_^)");
    }

2.3执行流程:

  1. 执行表达式,结果与case匹配

  2. 匹配成功哪条就执行,遇到break跳出

  3. case值都不匹配,执行default

public class SwitchDemo {
    public static void main(String[] args) {
        /**
         * Switc
         */
        //胡适之日记展示
        String weekDay = "周四";
        switch(weekDay){
            case "周一":
                System.out.println("读书");
                break;
            case "周二":
                System.out.println("打牌");
                break;
            case "周三":
                System.out.println("打牌");
                break;
            case "周四":
                System.out.println("胡适之啊,胡适之,你怎么如此堕落,要努力读书啊!");
                break;
            case "周五":
                System.out.println("打牌");
                break;
            default:
                System.out.println("沉迷打牌,不可自拔(^_^)");
        }
    }
}

3.循环结构

3.1 for循环

        /**
         * 求1-10的奇数和
         */
        int sum = 0;
        for (int i = 0; i < 10; i++) {
            if (i%2 ==1 ){
                sum+=i;
            }
        }

        for (int i = 0; i < 10; i+=2) {
                sum+=i;
        }

        /**
         * 水仙花数:三位数  and 个十百的数字立方和等于原数
         */
        for (int i = 100; i <999 ; i++) {
            int a = i/100;
            int b = i/10%10;
            int c = i%10;
            int count = 0;
            if ((a*a*a + b*b*b + c*c*c) == i){
                count++;
                System.out.println(i+"\t");
            }
            System.out.println("水仙花个数:"+count);
        }
    }

3.2 while循环

while和for功能上一样;

使用:直到循环多少次,用for;不知道多少次,while

/**
         * 纸张折叠多少次可以超过珠穆朗玛峰
         */
        double peakHeight=8848868;
        double paperThickness=0.1;
        int count=0;
        //while内条件成立才可以执行,完成后跳出
        while (paperThickness < peakHeight){
            paperThickness *= 2;
            count++;
        }
        System.out.println(count+"次,可以超过珠峰");
        //for代替
        //for (;paperThickness < peakHeight;){

3.3 do while循环

特点:一定会先执行一次循环体

/**
         * 抢票:
         *   先抢一次,再判断
         */
        int i=0;
        do {
            System.out.println("没抢到票,继续");
            i++;
        }while (i<3);
        System.out.println("抢票成功");

3.4总结

  • 三种循环区别:

    • for和while,先判断,后执行

    • do while 先执行一次,再判断

  • for和while区别:

    • 执行流程一样

    • 已知循环次数,用for;不知,while

    • for中,变量只能在循环内用;while结束后可以继续用;

4.死循环、循环嵌套

4.1死循环

  /**
         * 死循环
         *
         */
/*        for (;;){
            System.out.println("死循环");
        }*/

        //经典死循环
        while(true){
            System.out.println("死循环");
        }

/*        do {
            System.out.println("死循环");
        }while (true){
            System.out.println("死循环");
        }*/


 /**
         * 死循环:
         * 用户不断输入密码,直到正确
         */
        int PassWord = 520;
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("请输入正确密码:");
            int pass = scanner.nextInt();
            if (pass == PassWord){
                System.out.println("登录成功");
                break;
            }else {
                System.out.println("密码错误");
            }
        }

4.2循环嵌套

  • 循环嵌套

    • 循环包循环

  • 特点

    • 外部循环一次,内部循环全部

        /**
         * 嵌套循环
         *    五天,每天吃三顿饭
         */
        for (int i = 1; i <=5; i++) {
            System.out.println("第"+i+"天,开始");
            for (int j = 1; j <=3; j++) {
                System.out.println("吃第"+j+"顿饭");
            }
            System.out.println("=============第"+i+"天,结束==================");
        }

5.跳转控制语句

  • break:跳出并结束当前所在循环

  • continue:跳出当前循环的当次执行,进入下一次循环

    • 只能在循环中使用

 /**
         * 准备写5天日记,第三天后不写了,
         */
        for (int i = 1; i < 6; i++) {
            System.out.println(i+"天日记");
            if (i==3){
                break;
            }
        }
        System.out.println("========================");
        /**
         * 准备写5天日记,第三天忘了,之后继续
         */
        for (int i = 1; i < 6; i++) {
            if (i==3){
                continue;
            }
            System.out.println("第"+i+"天日记");
        }

6.随机数Random类

  • Random生成随机数特点:

    • random.nextInt(n);生成一个0到n-1的随机数

  • Random生成区间随机数技巧:

    • 减加法:random.nextInt(10)+1;生成1-10

/**
 * 猜字游戏
 */
public class RandomDemo1 {
    public static void main(String[] args) {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        //random.nextInt(n);生成一个0到n-1的随机数
        int i = random.nextInt(100)+1;
        System.out.println(i);
        while (true){
            System.out.println("请输入1-100间的数字:");
            int s = scanner.nextInt();
            if (s<0||s>100){
                System.out.println("请输入1-100范围的数字");
            }else if (s==i){
                System.out.println("恭喜你猜中了");
                break;
            }else if (s<i){
                System.out.println("猜的数略小");
            }else if (s>i){
                System.out.println("猜的数略大");
            }else {
                System.out.println("请输入正确的数字");
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值