流程控制语句 if

流程控制语句 if单分支

package Basic;

import java.util.Scanner;

public class Alcohol {
    public static void main (String[] args){
        System.out.println("请输入酒精含量");
        Scanner scanner = new Scanner(System.in);
        int alcohol =scanner.nextInt();


        if (alcohol >= 20 && alcohol < 80) {//加入双与(&&)符号判断酒精含量大于等于20时结果为false,就不在执行双与符号后的条件
            System.out.println("是酒驾!");
        } else if (alcohol>=80){
            System.out.println("是醉驾!");
        }else {
            System.out.println("没有违法!");
        }


    }
}
package Basic;

import java.util.Scanner;

public class Alcohol {
    public static void main (String[] args){
        System.out.println("请输入酒精含量");
        Scanner scanner = new Scanner(System.in);
        int alcohol =scanner.nextInt();


        if (alcohol >= 20 ) {//最先捕获大于20的条件,并执行是酒驾的结果,实际应该为醉驾!因为if条件排序不合理导致。
            System.out.println("是酒驾!");
        } else if (alcohol>=80){
            System.out.println("是醉驾!");
        }else {
            System.out.println("没有违法!");
        }


    }
}
请输入酒精含量
100
是酒驾!

Process finished with exit code 0

package Basic;

import java.util.Scanner;

public class Alcohol {
    public static void main (String[] args){
        System.out.println("请输入酒精含量");
        Scanner scanner = new Scanner(System.in);
        int alcohol =scanner.nextInt();


        if (alcohol >= 80 ) {//最先捕获大于80的条件,不成立就执行else if 后的语句。此if条件排序合理。
            System.out.println("是醉驾!");
        } else if (alcohol>=20){
            System.out.println("是酒驾!");
        }else {
            System.out.println("没有违法!");
        }


    }
}
请输入酒精含量
100
是醉驾!

Process finished with exit code 0

请输入酒精含量
75
是酒驾!

Process finished with exit code 0
    
请输入酒精含量
15
没有违法!

Process finished with exit code 0

posted @ 2022-04-24 13:54  追风的羊  阅读(36)  评论(0)    收藏  举报