zoukankan      html  css  js  c++  java
  • java-分支结构(四种基本分支结构的认识)

    分支结构:有条件的执行某语句,并非每句必走
    1)if结构:1条路
    2)if...else结构:2条路
    3)if...else if结构:多条路
    4)switch...case结构:多条路
    优点:效率高、结构清晰
    缺点:整数、相等
    break:跳出switch

    一、if结构的认识

    /*
    * 1.if结构:1条路
    * 1)语法:
    * if(boolean){
    * 语句块
    * }
    * 2)执行过程:
    * 判断boolean的值:
    * 若为true,则执行语句块
    * 若为false,则不执行语句块
    */

    例:

    char ch = 'o';
    if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'){
    System.out.println(ch+"是元音字母");
    }

    二、if...else结构

    /*
    * 2.if...else结构:2条路
    * 1)语法:
    * if(boolean){
    * 语句块1
    * }else{
    * 语句块2
    * }
    * 2)执行过程:
    * 判断boolean的值:
    * 若为true,则执行语句块1
    * 若为false,则执行语句块2
    */

    例:

    double price = 300.0; //消费金额
    if(price>=500){ //满500
    price = price*0.8; //打8折
    }else{ //不满500
    price = price*0.9; //打9折
    }
    System.out.println("消费金额为:"+price);

    三、if...else...if结构

    /*
    * 3.if...else if结构:多条路
    * 1)语法:
    * if(boolean-1){
    * 语句块1
    * }else if(boolean-2){
    * 语句块2
    * }else if(boolean-3){
    * 语句块3
    * }else{
    * 语句块4
    * }
    * 2)执行过程:
    * 判断boolean-1,若为true则执行语句块1,若为false则
    * 再判断boolean-2,若为true则执行语句块2,若为false则
    * 再判断boolean-3,若为true则执行语句块3,若为false则执行语句块4
    * 3)说明:
    * 3.1)块1234,只有可能走其中之一
    * 3.2)若没有最后的else,则块123并非必走其中之一
    * 3.3)若有最后的else,则块1234必走其中之一
    */
    例:
    int score = 45;
    if(score>=90){
    System.out.println("A-优秀");
    }else if(score>=80){
    System.out.println("B-良好");
    }else if(score>=60){
    System.out.println("C-中等");
    }else{
    System.out.println("D-不及格");
    }

    四、switch...case结构

    例:
    int num = 2;
    switch(num){
    case 1: //if(num==1)
    System.out.println(111);
    break;
    case 2: //以此为入口
    System.out.println(222);
    break; //跳出switch
    case 3:
    System.out.println(333);
    break;
    default:
    System.out.println(666);
    }

    更多精彩以后更新,转载注明!

    以后一定要见到自己想要的自己!
  • 相关阅读:
    [转]Apache与Tomcat的关系
    [转]学习object-c,补习一下指针
    [转]深度解析苹果成功的六大原因——公众类产品学习的榜样
    datetimepicker日期时间选择器
    缓缓回到顶部效果的实现
    bootstrap4实现点击标签展示对应的内容
    禁止页面内容滚动( 禁用 ctrl
    UI中的暗黑模式
    如何通过空状态留住用户
    交互设计常用原则和定律
  • 原文地址:https://www.cnblogs.com/CaiNiao-TuFei/p/7252774.html
Copyright © 2011-2022 走看看