• JAVA_SE基础——13.选择结构语句


    if选择结构
    语法:
    if(条件){
            代码块
    }
    

    public class Test{
            public static void main(String[] args){
                    int a = 5;
                    if(a < 6){
                            System.out.print("a小于6");
            }//输出结果为a大于b
        }
    }
    说明:if选择结构 条件必须是一个布尔表达式,一旦条件中的值为true就运行代码块,否则跳过


    if-else选择结构
    语法:
    if(条件){
            代码块1
    }else{
            代码块2
    }
    

    public class Test{
            public static void main(String[] args){
                    int a = 5;
                    if(a < 3){
                            System.out.print("a小于b");
                    }else{
                            System.out.print("a大于b");
                    }
        }//输出结果为a大于b
    }

    说明:if-else选择结构 一旦条件中的值为true时运行代码块1,否则运行代码块2



    多重if选择结构 
    语法:
    if(条件1){
            代码块1
    }else if(条件2){
            代码块2
    }else{
            代码块3
    }

    public class Test{
            public static void main(String[] args){
                    int a = 5;
                    if(a > 2){
                            System.out.print("a");
                    }else if(a > 3){
                            System.out.print("b");
                    }else if(a > 4){
                            System.out.print("c");
                    }
            }//输出结果是a
    }

    说明:多重if选择结构 解决须要推断的条件是连续的区间时有非常大优势,else if块能够有多个,取决于程序的须要。一旦条件1为true运行代码块1。否则运行else if块,推断条件2。为true时运行代码块2,否则运行代码块3,以此类推。当条件满足某个else if块则余下的将不再运行而跳出if块


    嵌套if选择结构 
    语法:
    if(条件1){
            if(条件2){
                    代码块1
            }else{
                    代码块2
            }
    }else{
            代码块3
    }

    public class Test{
            public static void main(String[] args){
                    int a = 3;
                    if(a > 2){
                            if(a != 3){
                                    System.out.print("a!=3");//代码块1
                            }else{
                                    System.out.print("a=3");//代码块2
                            }
                    }else{
                            System.out.print("a>2");//代码块3
                    }
            }//输出结果为a=3
    }

    说明:嵌套if选择结构 事实上就是在if选择结构里嵌入if选择结构,条件1为false时运行代码块3,否则运行内部if选择结构。也就是说要运行代码块1,则必须满足条件1及条件2

    switch选择结构 
    语法:
    public class Test{
    switch(表达式){
            case 常量1:
                    代码块1;
                    break;
            case 常量2:
                    代码块2;
                    break;
            default:
                    代码块3;
                    break;    
    }

    public class Test{
            public static void main(String[] args){
                    int a = 3;
                    switch (a){
                            case 1:
                              System.out.println("星期一");
                              break;
                            case 2:
                              System.out.println("星期二");
                              break;
                            case 3:
                              System.out.println("星期三");
                              break;
                            default:
                              System.out.println("周日");
                              break;
                    }//输出结果为星期三
            }
    }
    
    说明:switch选择结构
    表达式能够是整型变量或字符型变量
    case后必须是一个整型或字符型的常量表达式,一般是一个固定的字符、数字。case块能够有多个
    default块在其他case块都不满足情况下运行
    break表示跳出当前结构。即退出switch语句块
    与嵌套if选择结构相比,switch选择结构方便于解决等值推断问题

    不懂能够加Q联系我哦654249738



    if选择结构
    语法:
  • 相关阅读:
    如何确定软件测试结束?
    为什么会有软件测试这一栏目?
    SQL Server 新建 数据库关系图 时弹出警告提示此数据库没有有效所有者,因此无法安装数据库关系图支持对象。
    IIS 7 实现http跳转https 重定向方法
    Centos 利用yum安装卸载软件常用命令[转载]
    VMware虚拟机中Centos7的IP地址设置方法
    【3】工厂方法模式
    【2】简单工厂模式
    【1】Singleton模式(单例模式)
    SVN提交小结
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/6726986.html
Copyright © 2020-2023  润新知