【java基础】选择结构

一、选择结构

  1、if语句

int age  = 16;
if (age < 18) {
   System.out.println("未成年人不得进入!");
}

  2、if ... else语句  

int num = 13;
if (num % 2 == 0){
   System.out.println("偶数");
}else {
   System.out.println("奇数");
}

  3、if ... else if ...else

int result = 0;

if (80 <= result && result <= 120){
    System.out.println("优秀");;
}else if (60 <= result && result < 80 ){
    System.out.println("良");
}else if (0 <= result && result <60){
    System.out.println("不及格,继续努力");
}else{
    System.out.println("输入不合法");
}    

  4、switch语句 

        int num = 8;
        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("日期错误");
                break;
        }    

 

posted @ 2020-06-07 00:06  蹲马路牙子吃炒面  阅读(...)  评论(...编辑  收藏