java的分支语句if else elseif,

import java.util.Scanner;

public class TestIf {

    public static void main(String[] args) {
        /*
            单分支if语句:
            if(条件表达式){
                语句块
            }

            一旦遇到if,程序首先会对条件表达式进行判定,
            如果为true,则进入{}内部开始执行,执行结束以后,出了{}继续
            执行后面的代码;
            如果为false,则跳过{},执行{}后面的代码


         */
//        Scanner scan = new Scanner(System.in);
//        System.out.println("请输入第一个数:");
//        int num1 = scan.nextInt();
//        System.out.println("请输入第二个数:");
//        int num2 = scan.nextInt();
//        System.out.println("请输入第三个数:");
//        int num3 = scan.nextInt();
//        scan.close();
//
//        int max = num1;
//
//        if(max < num2){
//            max = num2;
//        }
//
//        if(max < num3){
//            max = num3;
//        }
//
//        System.out.println("最大的数是:" + max);


        /*
            双分支if语句
            if(条件表达式){
                语句块1
            }else{
                语句块2
            }

            执行顺序:
            首先判断条件表达式,
            如果为true则进入语句块1执行,执行完之后,跳到else的结束}外面,
            顺序往下;
            如果为false则进入语句块2执行,结束后跳到else的结束}外面,
            顺序往下。

         */
//        Scanner scan = new Scanner(System.in);
//        System.out.println("顾客你好,请输入你的年龄:");
//        int age = scan.nextInt();
//        scan.close();

//        if(age >= 18){
//            System.out.println("欢迎光临");
//        }else{
//            System.out.println("滚!");
//        }

        /*
            多分支if语句:
            if(条件表达式1){
                语句块1
            }else if(条件表达式2){
                语句块2
            }else if(条件表达式3){
                语句块3
            }.....{
            }else{
                语句块n
            }

            执行顺序:
            首先判断条件表达式1
            如果为true,则执行语句块1,然后跳到最后一个}的后面顺序往下执行;
            如果为false,则判断条件表达式2
            如果为true,则执行语句块2,然后跳到最后一个}后面顺序往下
            如果为false,则判断条件表达式3
            .....
            如果所有的条件表达式判断都为false,则进入最后一个else
            执行,然后出了最后一个}往下顺序执行

            注意:
            最后一个不带条件的else不是必须的,可以根据情况自主选择
         */
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入学生的成绩:");
        int score = scan.nextInt();
        scan.close();

//        if (score >= 0 && score <= 100) {
//            if (score >= 90) {
//                System.out.println("A");
//            } else if (score >= 80) {
//                System.out.println("B");
//            } else if (score >= 70) {
//                System.out.println("C");
//            } else if (score >= 60) {
//                System.out.println("D");
//            } else {
//                System.out.println("E");
//            }
//        } else {
//            System.out.println("成绩无效");
//        }

        if(score > 100 || score < 0){
            System.out.println("成绩无效");
        }else if (score >= 90) {
            System.out.println("A");
        } else if (score >= 80) {
            System.out.println("B");
        } else if (score >= 70) {
            System.out.println("C");
        } else if (score >= 60) {
            System.out.println("D");
        } else {
            System.out.println("E");
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值