• 基础语法-判断结构if语句


                      基础语法-判断结构if语句

                                           作者:尹正杰

    版权声明:原创作品,谢绝转载!否则将追究法律责任。

     

     

    一.单分支语句

    /**
     *     判断结构if单分支语句
     * @author 尹正杰
     *
     */
    public class IfDemo1 {
    
        public static void main(String[] args) {
            int x = 100;
            
            
            if(x >20) {
                System.out.println("大于");
            }
            
            //if单分支的简写形式,如果if中控制的只有一行代码,那么可以把"{}"省略掉,如下所示:(生产环境不推荐这样写,可读性太差!)
            if(x>20) System.out.println("大于");
            
            System.out.println("继续执行");
            
            if(x < 20) {
                System.out.println("小于");
            }
            
            //if单分支的简写形式,如果if中控制的只有一行代码,那么可以把"{}"省略掉,如下所示:(生产环境不推荐这样写,可读性太差!)
            if(x < 20)
                System.out.println("小于");
            
            System.out.println("执行完毕");
        }
    
    }

    二.双分支语句

    /**
     *     判断结构if双分支语句
     * @author 尹正杰
     *
     */
    public class IfDemo2 {
    
        public static void main(String[] args) {
            int x = 100,y = 200;
            
            if(x > y) {
                System.out.println("x大于 y");
            }else {
                System.out.println("x小于y");
            }
            
            //使用三元运算符可以改写上面的双分支语句
            String res = (x > y)?"x大于 y":"x小于y";
            
            System.out.println(res);
            
        }
    
    }

    三.多分支语句

    /**
     *     判断结构if多分支语句
     * @author 尹正杰
     *
     */
    public class IfDemo3 {
    
        public static void main(String[] args) {
            int soure = 95;
            
            if(soure > 90) {
                System.out.println("稳住,别浪!");
            }else if (soure > 80) {
                System.out.println("加油吧,少年~");
            }else if (soure > 70) {
                System.out.println("继续努力,争取下次更上一层楼");
            }else if (soure > 60) {
                System.out.println("你还有很大的进步空间哟,加油!");
            }else {
                System.out.println("再不努力就成为吊车尾啦~");
            }
        }
    
    }

    四.嵌套if语句

    /**
     *     嵌套if语句
     * @author 尹正杰
     *
     */
    public class IfDemo4 {
        public static void main(String[] args) {
            String Gender = "girl";
            byte Age = 18;
            
            if (Gender == "girl") {
                if (Age <= 18) {
                    System.out.println("锁门");
                }
            }else if (Gender == "boy") {
                System.out.println("开门");
            }else {
                System.out.println("不开门");
            }
        }
    }

    五.判断结构

      三元运算符和if...else的区别:
        三元运算符是一个运算符,所以运行必须有结果,而if...else语句只能控制流程,所以不一定有结果。

      某些情况下,可以把if...else语句改写成三元运算符的形式:
        前提是要保证if...else执行完有具体的结果出现。

      if语句特点:
        每一种格式都是单条语句;
        第二种格式与三元运算符的区别:三元运算符运算完要要有值出现;
        条件表达式无论写成什么样子,只看最终的结构是否是true或者false。

     

     

     

     

  • 相关阅读:
    .Net Core ----通过XUnit进行接口单元测试(带请求头及参数)并用output输出结果
    .Net Core---- 通过EPPlus批量导出
    .Net Core---- 自带Json返回日期带T格式 解决
    You need tcl 8.5 or newer in order to run the Redis test
    exec: "docker-proxy": executable file not found in $PATH
    docker 如何清理垃圾呢
    docker run 报错——WARNING: IPv4 forwarding is disabled. Networking will not work.
    go 依赖包管理工具gb安装报错
    keepalived实现nginx高可用
    php命令行查看扩展信息
  • 原文地址:https://www.cnblogs.com/yinzhengjie2020/p/12212787.html
Copyright © 2020-2023  润新知