javase复习day04

1.流程控制语句

        通过一些语句,控制程序的执行流程。

1.1顺序结构

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

1.2分支结构

分类

  • if语句
  • Switch语句

1.2.1 if语句

1.2.1.1 if格式一

注意

  • 大括号的开头可以另起一行书写,打包建议书写在第一行的末尾。
  • 在语句体中,如果只有一行代码,大括号可以省略不写,个人建议,大括号不要省略。
  • 如果对一个布尔变量进行判断,不要使用==号,直接写在小括号即可。

练习

    public static void main(String[] args) {
//        定义变量记录小明的名次
        int ranking = 1;
//        判断小明是否是第一
        if (ranking == 1){
            System.out.println("小红是小明的女朋友");
        }
    }

练习:

        

    public static void main(String[] args) {
        boolean islightGreen = true;
        boolean islightYellow = false;
        boolean islightRed = false;

        if (islightGreen){
            System.out.println("GOGOGO");
        }
        if (islightYellow){
            System.out.println("Show");
        }
        if (islightRed){
            System.out.println("Stop");
        }

    }
1.2.1.2 if格式二

练习:

    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        System.out.println("请录入身上的钱");
        int money = sc.nextInt();

        if (money>100){
            System.out.println("吃网红餐厅");
        }else {
            System.out.println("吃沙县小吃");
        }
    }

练习:

    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        System.out.println("请录入实际支付的钱");
        int many = sc.nextInt();

        if (many>600){
            System.out.println("付款成功");
        }else {
            System.out.println("付款失败");
        }
    }

练习:

    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        System.out.println("请录入电影票的票号");
        int ticket = sc.nextInt();
        //票号0-100之间
        if (ticket>=0 && ticket<=100){
            if (ticket%2==0){
                System.out.println("座左边");
            }else {
                System.out.println("座右边");
            }
        }
    }
1.2.1.3 if的第三种格式

        

练习:

    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        System.out.println("请录入小明的成绩");
        int price = sc.nextInt();
        //对成绩进行校验
        if (price>=0 && price<=100){
            if (price>=95 && price<=100){
                System.out.println("送自行车");
            } else if (price>=90 && price<=94) {
                System.out.println("送游乐园");
            } else if (price>=80 && price<=89) {
                System.out.println("送变形金刚");
            }else{
                System.out.println("送竹笋炒肉");
            }
        }else {
            System.out.println("当前成绩不合法");
        }
    }

练习:

    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        System.out.println("请录入会员的级别");
        int vip = sc.nextInt();
        int price = 1000;
        //对成绩进行校验
        if (vip==1||vip==2||vip==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);
            }
        }else {
            System.out.println("录入不合格");
            System.out.println(price);
        }
    }

练习:

    public static void main(String[] args) {
        boolean islightGreen = true;
        boolean islightYellow = false;
        boolean islightRed = false;

        if (islightGreen){
            System.out.println("GOGOGO");
        } else if (islightYellow){
            System.out.println("Show");
        } else if (islightRed){
            System.out.println("Stop");
        }
    }

1.2.2 Switch语句

练习:

        

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入今天是星期几");
        String week = sc.next();

        switch (week){
            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;
            case "星期六":
                System.out.println("爬山");
                break;
            case "星期日":
                System.out.println("好好吃一顿");
                break;
            default:
                System.out.println("输入的数据错误,请重新输入");
                break;
        }
    }

1.2.2.2switch其他知识点

 defaule:

  • 位置:defaule不一定写在最下边,可以写在任意位置,习惯写在最下边。
  • 省略:defaule可以省略,语法不会有问题,但是不建议。 

case穿透:

        语句中没有写break导致。

执行流程;

        首先与所有的case的值匹配,匹配上了就执行对应的语句,如果此时发现break则跳出switch,如果没有发现,则继续寻找下一个case语句,直到找到break或者大括号为止。

        使用场景:

                如果多个case语句体重复了,可以使用case穿透简化代码。

      switch的新特性(jdk12)

    public static void main(String[] args) {
//        int n = 1;
//        switch (n){
//            case 1:
//                System.out.println("yi");
//                break;
//            case 2:
//                System.out.println("er");
//                break;
//            case 3:
//                System.out.println("san");
//                break;
//            default:
//                System.out.println("wu");
//                break;
//        }
//jdk12的新特性
//        int n = 1;
//        switch (n){
//            case 1->{
//                System.out.println("yi");
//            }
//            case 2->{
//                System.out.println("er");
//            }
//            case 3->{
//                System.out.println("san");
//            }
//            default->{
//                System.out.println("wu");
//            }
//        }

//        化简
        int n = 1;
        switch (n){
            case 1->System.out.println("yi");
            case 2->System.out.println("er");
            case 3->System.out.println("san");
            default->System.out.println("wu");
        }
    }

switch和if的第三种模式的各自的使用场景:

        if的第三种模式,一般用于对范围的判断。

        switch:把有限个数据一一列举出来,让我们任选其一。

练习

        

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入星期");
        int a = sc.nextInt();

//        switch (a){
//            case 1:
//            case 2:
//            case 3:
//            case 4:
//            case 5:
//                System.out.println("工作日");
//                break;
//            case 6:
//            case 7:
//                System.out.println("休息日");
//                break;
//            default:
//                System.out.println("没有这一天");
//                break;
//        }


//        switch (a){
//            case 1,2,3,4,5:
//                System.out.println("工作日");
//                break;
//            case 6,7:
//                System.out.println("休息日");
//                break;
//            default:
//                System.out.println("没有这一天");
//                break;
//        }

        switch (a){
            case 1,2,3,4,5->System.out.println("工作日");
            case 6,7->System.out.println("休息日");
            default->System.out.println("没有这一天");
        }
    }

练习:

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的选择");
        int choose = sc.nextInt();
        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("取消操作");
        }
    }

        

1.3循环结构

练习:

    public static void main(String[] args) {
        int x = 123321;
//        用于存反转之后的数
        int num = 0;
//        定义临时变量用于记录x的值
        int y = x;
        while (x != 0){
//            从右往左获取每一位数字
            int ge = x%10;
//            修改记录的值,将x的最后一位去掉,即除以10
            x= x/10;
//            将获取到的数添加到反转的数上
            num = num*10 + ge;

            System.out.println(ge);
        }
        System.out.println(num);
        System.out.println(num==y);
    }

练习:

        

    public static void main(String[] args) {
        int beichushu = 10;
        int cushu = 3;
        int count = 0;
        while (beichushu>=cushu){
            beichushu =beichushu -cushu;
            count++;
        }
        System.out.println("余数 "+beichushu);
        System.out.println("商 "+count);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值