• e1086. if/else语句


    The if statement encloses some code which is executed only if a condition is true. The general syntax of the if statement is:

        if (condition) {
            body-code
        }
    

    The if statement has two parts. The condition is a boolean expression which resolves to either true or false. The body-code is code that will be executed only if the condition is true. Here is an example of the if statement.

        // Returns 0 if s is null
        public int getLength(String s) {
            if (s == null) {
                // This line of code is execute only if s is null
                return 0;
            }
            return s.length();
        }
    

    The if statement also has an optional else part which encloses some code that is executed only if the condition is false. Here is the general syntax of the if statement with the optional else part:

        if (condition) {
            body-code
        } else {
            else-code
        }
    

    The else-code is executed only if condition resolves to false. Here is an example of the if statement with the optional else part.

        // Generates a random number and prints whether it is even or odd
        if ((int)(Math.random()*100)%2 == 0) {
            System.out.println("The number is even");
        } else {
            System.out.println("The number is odd");
        }
    

    The else-code part can also be another if statement. An if statement written this way sets up a sequence of condition's, each tested one after the other in top-down order. If one of the conditions is found to be true, the body-code for that if statement is executed. Once executed, no other conditions are tested and execution continues after the sequence. Here is an example of a sequence of if/else/if statements

        // Converts the time, which is in milliseconds, into a
        // more readable format with higher units
        public String getTimeUnit(long time) {
            String unit = "ms";
            if (time >= 365*24*60*60*1000L) {
                unit = "years";
                time /= 365*24*60*60*1000L;
            } else if (time >= 24*60*60*1000) {
                unit = "days";
                time /= 24*60*60*1000;
            } else if (time >= 60*60*1000) {
                unit = "hr";
                time /= 60*60*1000;
            } else if (time >= 60*1000) {
                unit = "min";
                time /= 60*1000;
            } else if (time >= 1000) {
                unit = "sec";
                time /= 1000;
            }
            // Execution continues here if any of the conditions match
            return time + " " + unit;
        }
    

    Here's some sample output:

        getTimeUnit(123);                  // 123 ms
        getTimeUnit(1234));                // 1 sec
        getTimeUnit(12345));               // 12 sec
        getTimeUnit(123456));              // 2 min
        getTimeUnit(1234567));             // 20 min
        getTimeUnit(12345678));            // 3 hr
        getTimeUnit(123456789));           // 1 days
        getTimeUnit(1234567890));          // 14 days
        getTimeUnit(12345678900L));        // 142 days
        getTimeUnit(123456789000L));       // 3 years
    
    Related Examples
  • 相关阅读:
    sourcenav安装
    vim-addon-manager【转】
    zmq重点
    一个简单的解决方法:word文档打不开,错误提示mso.dll模块错误。
    微软验证码项目 Captcha Code Demo 从 .NET Core 1.1.2升级到2.1.0
    海康设备如何接入萤石开放平台
    ABP 启用多租户实现数据隔离
    Docker 开发者常用操作命令
    .NET Core 深度克隆对象,引用类型的平行世界
    详解 .NET Core 遍历 List 并移除项
  • 原文地址:https://www.cnblogs.com/borter/p/9575292.html
Copyright © 2020-2023  润新知