侧边栏壁纸
博主头像
青菜-halo2 博主等级

行动起来,活在当下

  • 累计撰写 74 篇文章
  • 累计创建 6 个标签
  • 累计收到 7 条评论

目 录CONTENT

文章目录

03-流程控制语句

Administrator
2025-06-02 / 0 评论 / 0 点赞 / 26 阅读 / 0 字

一、顺序结构

java 程序默认执行的流程,按照代码的先后顺序,从上到下依次执行

package com.yq.orderdemo;
​
public class orderDemo {
    public static void main(String[] args) {
        System.out.println("努力做主人喜欢的事");
        System.out.println("大小姐驾到!统统闪开!");
        System.out.println("凛冬已至,故乡的梅花开了吗");
        System.out.println("心怀不惧,方能翱翔于天际");
    }
}
​
/* 输出结果:
努力做主人喜欢的事
大小姐驾到!统统闪开!
凛冬已至,故乡的梅花开了吗
心怀不惧,方能翱翔于天际
*/

二、分支结构

1、if

1.1 if 第一种格式

适合只有一种判断

if (关系表达式) {
    语句体;
} 
​
if (酒量 > 2) {
    System.out.println("小伙子,不错!");
}
​
/* 执行流程:
1、首先计算关系表达式的值
2、如果辨析表达式的值为true,则执行语句体
3、如果关系表达式的值为false,则不执行语句体
4、继续执行后面的其他语句
*/

// 需求:定义变量记录女婿的酒量,如果女婿的酒量大于2斤,老丈人就搭理他,否则就不搭理
package com.yq.ifdemo;
​
import java.util.Scanner;
​
public class IfDemo1 {
    public static void main(String[] args) {
        // 需求:键盘录入记录女婿的酒量,如果女婿的酒量大于2斤,老丈人就搭理他,否则就不搭理
        /* if格式
        if (关系表达式) {
            语句体;
        }
        */
​
        // 1、键盘录入女婿的酒量
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入女婿的酒量");
        int wine = sc.nextInt();
​
        // 2、对酒量进行判断
        if (wine > 2){
            System.out.println("小伙子,不错!");
        }
    }
}

1.2 if 语句的注意事项

package com.yq.ifdemo;
​
/*
    if (关系表达式) {
        语句体;
    }
​
    if的注意点:
        1、大括号的开头可以另起一行,但是建议写在第一行的末尾
        2、语句体中,如果只有一句代码,大括号可以省略不屑(建议大括号不要省略)
        3、如果对一个布尔值的变量进行判断,不要用 == 号,直接把变量卸载小括号中即可
*/
​
public class IfDemo2 {
    public static void main(String[] args) {
        /*
        int number = 10;
        if (number >= 10 ) {
            // int a = 100; //1.定义变量a;2.给变量a赋值为100
            System.out.println("number大于等于10");
        }
        */
​
        boolean flag = false;
        if (flag) {
            System.out.println("flag的值为true");
        }
    }
}

1.3 练习-名次判断

package com.yq.test;
​
public class Test1 {
    public static void main(String[] args) {
        // 小红:如果你这次考试全班第一,我就做你女朋友
​
        //分析:
        // 1、定义变量记录小明的名词
        int ranking = 2;
​
        // 2、对小明的名词进行判断
        if (ranking == 1) {
            System.out.println("小红成为了小明的女朋友");
        }
    }
}

1.4 练习-自动驾驶

红灯停,绿灯行

package com.yq.test;
​
public class Test2 {
    public static void main(String[] args) {
        //汽车无人驾驶会涉及大量的判断
        //当汽车行驶的时候遇到红绿灯,就会进行判断
        // 红灯停、绿灯行、黄灯减速
​
        //1、定义三个变量表示等的状态
        // true亮  false灭
        boolean isLightGreen = false;
        boolean isLightYellow = false;
        boolean isLightRed = true;
​
        //2、判断  红灯停、绿灯行、黄灯减速
        if (isLightGreen) {
            System.out.println("go go go!!!");
        }
        if (isLightYellow) {
            System.out.println("slow!!");
        }
        if (isLightRed) {
            System.out.println("stop!!");
        }
    }
}

1.5 if 语句的第二种格式

适合只有两种判断

if (关系表达式) {
    语句体1;
} else {
    语句体2;
}
​
/*
执行流程:
1、首先计算关系表达式的值
2、如果关系表达式的值为true,就执行语句体1
3、如果关系表达式的值为false,就执行语句体2
4、继续执行后面的其他语句
*/

1.6 练习-吃饭

package com.yq.test;
​
import java.util.Scanner;
​
public class Test3 {
    public static void main(String[] args) {
        /*
        需求:键盘录入一个正数,表示身上的钱
        如果大于等于100块,就吃网红餐厅。否则就吃经济实惠的沙县小吃
         */
​
        //1、键盘录入,表示身上的钱
        Scanner sc = new Scanner(System.in);
        System.out.println("请录入身上的钱");
        int money = sc.nextInt();
​
        //2、对钱进行判断(二选一)
        if (money >= 100) {
            System.out.println("吃网红餐厅");
        } else {
            System.out.println("吃经济实惠的沙县小吃");
        }
    }
}

1.7 练习-商品付款

package com.yq.test;

import java.util.Scanner;

public class Test4 {
    public static void main(String[] args) {
        /* 在实际开发环境中,如果根据两种不同的情况来执行不同的代码,就需要用到if的第二种格式
        需求:
        假设,用户在超市实际垢面,商品总价为:600元
        键盘录入一个整数,表示用户实际支付的钱。
        如果付款大于等于600,表示付款成功。否则付款失败
        */

        //1、键盘录入一个整数,表示用户实际支付的钱。
        Scanner sc = new Scanner(System.in);
        System.out.println("录入一个整数表示实际支付的钱");
        int money = sc.nextInt();

        //2、判断
        if (money >= 600) {
            System.out.println("付款成功");
        } else {
            System.out.println("付款失败");
        }
    }
}

1.8 练习-影院选座

package com.yq.test;

import java.util.Scanner;

public class Test5 {
    public static void main(String[] args) {
        /*在实际开发中,电影院选座也会使用到if判断。
        假设某影院售卖了100张票,票的序号为1~100。
        其中奇数票号坐左侧,偶数票号坐右侧。
        键盘录入一个整数表示电影票的票号。
        根据不同情况,给出不同的提示:
        如果票号为奇数,那么打印坐左边
        如果票号为偶数,那么打印坐右边。*/

        //1.键盘录入一个整数表示电影票的票号。
        Scanner sc = new Scanner(System.in);
        System.out.println("请录入一个票号");
        int ticket = sc.nextInt();

        //2.判断票号为奇数,还是偶数
        //只有当ticket 在0-100之间,才是真实有效的票
        if (ticket >= 0 && ticket <=100 ) {
            if (ticket % 2 == 1) {
                System.out.println("坐左边");
            } else {
                System.out.println("坐右边");
            }
        } else {
            System.out.println("输入的票号不对,请输入 0 ~ 100 之间的数");
        }
    }
}

1.9 if 语句的第三种格式

适合只有多种判断

if (关系表达式1) {
    语句体1;
} else if (关系表达式2) {
    语句体2;
}
...
  else {
      语句体 n + 1;
}

/*
执行流程:
1、首先计算关系表达式1的值
2、如果为true,就执行语句体1;如果为false,就计算关系表达式2的值
3、如果true,就执行语句体2;如果为false就计算关系表达式3的值
4、。。。
5、如果所有关系表达式结果都为false,就执行语句体 n + 1。
*/

1.10 练习-根据成绩给不同奖励

package com.yq.test;

import java.util.Scanner;

public class Test6 {
    public static void main(String[] args) {
        /*
        根据不同的分数送不同的礼物。
        如果是95~100分,送自行车一辆
        如果是90~94分,游乐场玩一天
        如果是80~89分,送变形金刚一个。
        如果是80分,揍一顿。*/

        //1、键盘录入小明的考试成绩
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数,表示小明的成绩");
        int score = sc.nextInt();

        //对异常数据进行判断
        //2、根据不同的成绩,给出不同的奖励
        if (score > 0 && score <= 100) {
            if (score >= 95 && score <= 100) {
                System.out.println("送自行车一辆");
            } else if (score >=90 && score <= 94) {
                System.out.println("游乐场玩一天");
            } else if (score >= 80 && score <= 89) {
                System.out.println("送变形金刚一个");
            } else {
                System.out.println("揍一顿");
            }
        } else {
            System.out.println("当前录入的成绩不合法");
        }
    }
}

1.11 练习-商品的价格

package com.yq.test;

import java.util.Scanner;

public class Test7 {
    public static void main(String[] args) {
        /*在实际开发中,多种情况判断时,会用到if的第三种格式:
        需求:
            商场都会有VIP的会员制,根据不同的会员会有不同的折扣。
            假设商品总价为1000。
            键盘录入会员级别,并计算出实际支付的钱。
            会员1级:打9折。
            会员2级:打8折。
            会员3级:打7折。
            非会员:不打折,要打也是打骨折。*/

        //1、定义变量记录商品的总价
        int price = 1000;

        //2、键盘录入会员的级别
        Scanner sc = new Scanner(System.in);
        System.out.println("请录入会员的级别( 0-3 )");
        int vip = sc.nextInt();

        //3、根据级别来计算实际要支付的钱
        if (vip == 1){
            System.out.println("实际支付的钱为" + (price * 0.9));
        } else if (vip == 2) {
            System.out.println("实际支付的钱为" + (price * 0.8));
        } else if (vip == 3) {
            System.out.println("实际支付的钱为" + (price * 0.7));
        } else {
            System.out.println("实际支付的钱为" + price);
        }
    }
}

1.12 练习-自动驾驶

package com.yq.test;

public class Test8 {
    public static void main(String[] args) {
        //汽车无人驾驶会涉及大量的判断
        //当汽车行驶的时候遇到红绿灯,就会进行判断
        // 红灯停、绿灯行、黄灯减速

        //1、定义三个变量表示等的状态
        // true亮  false灭
        boolean isLightGreen = false;
        boolean isLightYellow = false;
        boolean isLightRed = true;

        //2、判断  红灯停、绿灯行、黄灯减速
        if (isLightGreen) {
            System.out.println("go go go!!!");
        } else if (isLightYellow) {
            System.out.println("slow!!");
        } else if (isLightRed) {
            System.out.println("stop!!");
        }
    }
}

2、switch

switch(表达式) {
    case 值1:
        语句体1;
        break;
    case 值2:
        语句体2;
        break;
    ...

    default:
        语句体 n+1;
        break;
}


/*
格式说明:
1、表达式:(将要匹配的值)取值为byte、short、int、char
		jdk5以后可以是枚举,jdk7以后可以是String
2、case:后面跟的是要和表达式进行比较的值(被匹配的值)
3、break:表示中断,结束的意思,用来结束switch语句
4、default:表示所有情况都不匹配的时候,就执行该处的内容,和if语句的else相似
5、case后面的值之恩那个是字面量,不能是变量
6、case给出的值不允许重复
*/

2.1 练习-选择吃什么面

package com.yq.switchdemo;

public class SwitchDemo1 {
    public static void main(String[] args) {
        //兰州拉面、武汉热干面、北京炸酱面、陕西油泼面
        //1、定义变量记录想吃的面
        String noodles = "陕西油泼面";

        //2、与四种面条进行匹配
        switch (noodles) {
            case "兰州拉面":
                System.out.println("吃兰州拉面");
                break;
            case "武汉热干面":
                System.out.println("吃武汉热干面");
                break;
            case "北京炸酱面":
                System.out.println("吃北京炸酱面");
                break;
            case "陕西油泼面":
                System.out.println("吃陕西油泼面");
                break;
            default:
                System.out.println("吃方便面");
                break;
        }
    }
}

2.2 运动计划

package com.yq.test;

import java.util.Scanner;

public class Test9 {
    public static void main(String[] args) {
        /*需求:键盘录入星期数,显示今天的减肥活动。
        周一:跑步
        周二:游泳
        周三:慢走
        周四:动感单车
        周五:拳击
        周六:爬山
        周日:好好吃一顿*/

        //1、键盘录入星期几
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入星期几");
        int week = sc.nextInt();

        //2、利用switch进行匹配
        switch (week) {
            case 1:
                System.out.println("跑步");
                break;
            case 2:
                System.out.println("游泳");
                break;
            case 3:
                System.out.println("慢走");
                break;
            case 4:
                System.out.println("动感单车");
                break;
            case 5:
                System.out.println("拳击");
                break;
            case 6:
                System.out.println("爬山");
                break;
            case 7:
                System.out.println("好好吃一顿");
                break;
            default:
                System.out.println("没有这个星期");
                break;
        }
    }
}

2.3 switch 其他知识点

2.3.1 default 的位置和省略
public class SwitchDemo2 {
    //1、位置default 与其他case无顺序要求,但习惯在最下面
    //2、default可以省略,语法无问题,但不建议省略

    public static void main(String[] args) {
        int number = 100;
        switch (number) {
            //default:
                //System.out.println("number数值不是1,也不是10,也不是20");
                //break;
            case 1:
                System.out.println("number数值为1");
                break;
            case 10:
                System.out.println("number数值为10");
                break;
            case 20:
                System.out.println("number数值为20");
                break;
        }
    }
}

2.3.2 case 穿透
public class SwitchDemo3 {
    public static void main(String[] args) {
        //case穿透:case语句中break省略导致的
        //执行流程:用小括号中的表达式匹配下面的每个case,若匹配上了,则会执行对应语句体
            //此时如有break,则结束整个switch;若没有break,则继续下一个case,知道switch结束

        //使用场景:若有有多个case语句执行效果重复,可以考虑case穿透简化代码

        int number = 10;
        switch (number) {
            case 1:
                System.out.println("number数值为1");
                break;
            case 10:
                System.out.println("number数值为10");
                //break;
            case 20:
                System.out.println("number数值为20");
                break;
            default:
                System.out.println("number数值不是1,也不是10,也不是20");
                //break;
        }
    }
}

2.3.3 switch 新特性
public class SwitchDemo4 {
    public static void main(String[] args) {
        //switch新特性 ->
        //条件:jdk12以上

        int number = 4;
        /*
        switch (number) {
            case 1:
                System.out.println("一");
                break;
            case 2:
                System.out.println("二");
                break;
            case 3:
                System.out.println("三");
                break;
            case 4:
                System.out.println("四");
                break;
            case 5:
                System.out.println("五");
                break;
            default:
                System.out.println("没有这个选项");
                break;
        } */

        /*switch (number) {
            case 1 -> {
                System.out.println("一");
            }
            case 2 -> {
                System.out.println("二");
            }
            case 3 -> {
                System.out.println("三");
            }
            case 4 -> {
                System.out.println("四");
            }
            case 5 -> {
                System.out.println("五");
            }
            default -> {
                System.out.println("没有这个选项");
            }
        } */

        // 若case中语句体只有一行,则可以省略
        switch (number) {
            case 1 -> System.out.println("一");
            case 2 -> System.out.println("二");
            case 3 -> System.out.println("三");
            case 4 -> System.out.println("四");
            case 5 -> System.out.println("五");
            default -> System.out.println("没有这个选项");
        }
    }
}

2.3.4 switchif
public class SwitchDemo5 {
    public static void main(String[] args) {
        //switch 和 if的第三种格式 各自的使用场景
            // if适用于范围类的
            // switch适用于一一列举的
        int score = 100;
        if (score >= 90 && score <=100) {
            System.out.println("送自行车");
        }

        //此处为分数范围,也可用switch,但90~100范围太多,使用switch不合适
    }
}

2.4 练习-工作休息日判断

import java.util.Scanner;

public class Test10 {
    public static void main(String[] args) {
        // 需求:录入星期数,实现输出 (1-5)工作日 (6-7)休息日
        //1、录入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入星期数");
        int week = sc.nextInt();

        //2、判断 (此处用到了 switch的穿透(case语句执行效果重复,考虑case穿透简化代码))
        /*
        switch (week) {
            case 1:
                System.out.println("工作日");
            case 2:
                System.out.println("工作日");
            case 3:
                System.out.println("工作日");
            case 4:
                System.out.println("工作日");
            case 5:
                System.out.println("工作日");
            case 6:
                System.out.println("休息日");
                break;
            case 7:
                System.out.println("休息日");
                break;
            default:
                System.out.println("没有这个选项");
                break;
        } */

        //优化
        switch (week) {
            case 1,2,3,4,5 -> System.out.println("工作日");
            case 6,7 -> System.out.println("休息日");
            default -> System.out.println("没有这个选项");
        }
    }
}

2.5 机票服务

import java.util.Scanner;
public class Test11 {
    public static void main(String[] args) {
       /* 在实际开发中,如果我们需要在多种情况下选择其中一个,就可以使用switch语句。
        当我们拨打了某些服务电话时,一般都会有按键选择。
        假设现在我们拨打了一个机票预定电话。
        电话中语音提示:
        1机票查询
        2机票预订
        3机票改签
        4退出服务
        其他按键也是退出服务。请使用swtich模拟该业务逻辑。*/

        //1、录入
        Scanner sc = new Scanner(System.in);
        System.out.println("1 机票查询\n" +
                           "2 机票预订\n" +
                           "3 机票改签\n" +
                           "4 退出服务");
        System.out.println("请输入选项");
        int choose = sc.nextByte();

        //2、判断
        switch (choose) {
            case 1 -> System.out.println("机票查询");
            case 2 -> System.out.println("机票预订");
            case 3 -> System.out.println("机票改签");
            //case 4 -> System.out.println("退出服务");
            default -> System.out.println("退出服务");
        }
    }
}

三、循环结构


0

评论区