第五天学习Java的笔记(if,switch顺序结构)

还有51天

流程概述和顺序结构

每条语句的执行流程。

1.1 顺序结构

//顺序结构
public class Demo01Sequence {
    public static void main(String[] args) {
        System.out.println("1");
        System.out.println("2");
        System.out.println("3");//1,2,3
    }
}

1.2 判断

if语句

//单if语句
public class Demo02If {
    public static void main(String[] args) {
        System.out.println("发现网吧");
        int age = 19;
        if (age >= 18) {
            System.out.println("进入网吧");
            System.out.println("遇见猪队友");
            System.out.println("结账走人")}
        System.out.println("回家");
    }
}

if…else语句

//标准的if-else语句
public classDemo03IfElse {
    public static void main(String[] args) {
        int num = 1;
        if (num % 2 == 0) {//不可以这部分写成num%2,然后判断布尔值,会报错int无法转为boolean
            System.out.println("偶数");
        } else {
            System.out.println("奇数");
        }
    }
}

if…else if…else

//x >= 3;y = 2x + 1
//-1 < x <3,y = 2x;
//x <= -1,y = 2x - 1;
public class Demo04IfElseExt {
    public static void main(String[] args) {
        int x = 4;
        int y;
        if (x >= 3) {
            y = 2 * x + 1;
        } else if (x > -1 && x < 3) {
            y = 2 * x;
        } else {
            y = 2 * x - 1;
        }
        System.out.println("结果是:" + y);//9
    }
}

成绩划分练习

/*
90-100优秀;80-89好;70-79良;60-69及格;60以下不及格;>100或者<0数据错误
*/
public class Demo05IfElsePratice {
    public static void main(String[] args) {
        int score = 97;
        if (score >= 90 && score <= 100){
            System.out.println("优秀");
        } else if (score >= 80 && score < 90) {
            System.out.println("好");
        } else if (score >= 70 && score < 80) {
            System.out.println("良");
        } else if (score >= 60 && score < 70) {
            System.out.println("及格");
        } else if (score >= 0 && score < 60) {
            System.out.println("不及格");
        } else {
            System.out.println("数据不正确");
        }
    }
}

用if语句代表三元运算符

//使用三元运算符和标准的if-else语句分别实现:取两个数字当中的最大值
public class Demo06Max {
    public static void main(String[] args) {
        int a = 10,b = 20;
        //使用三元运算符
        //int max = a > b ? a : b;
        //System.out.println("最大值:" + max);
        if max;
        if (a > b){
            max = a;
        } else {
            max = b;
        }
        System.out.println("最大值:" + max);
    }
}

1.3 选择

switch语句

public class Demo07Switch {
    public static void main(String[] args){
        int num = 1;
        switch (num) {
            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("error");
                break;//可以省略,但是不建议省略,因为各个case顺序可以变,万一颠倒了default不是最后一个,可能就会穿透其他case
        }
    }
}

Switch语句的注意事项:

/*
1.多个case后面的数值不可以重复,报错case标签重复
2.switch后面小括号当中只能是下列数据类型:基本数据类型:byte/short/char/int
引用数据类型:String字符串、enum枚举
3.switch语句格式可以很灵活:前后顺序可以颠倒,而且break语句可以省略
“匹配到哪一个case就从哪一个位置向下执行,直到遇到了break或者整体结束为止。”
*/
public class Demo08SwitchNotice {
    public static void main(String[] args) {
        int num = 2;
        switch (num){
            case 1:
                System.out.println(1);
                break;
            case 2:
                System.out.println(2);
                //break;
            case 3:
                System.out.println(3);
                break;
            default:
                System.out.println("error");
                break;
        }//会输出2 3
    }
}
  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值