第二章:流程控制语句

  1. 三种控制结果分为是:顺序,条件,循环

    1. 顺序语句:顺序执行,根据编写的顺序,从上到下运
    public static void main(String[] args){
    //顺序执行,根据编写的顺序,从上到下运行
    System.out.println(1);
    System.out.println(2);
    System.out.println(3);
    }
    
    1. 条件语句:
    if(关系表达式){
    语句体;
    }
    public class Flow_Control_Statements {
    
        public static void main(String[] args) {
    
            // if 条件语句
            System.out.println("---开始if条件语句---");
            // 定义两个变量
            int a = 10;
            int b = 20;
            int c = 10;
    
            if(关系表达式){
    语句体;
    }
    
            if(a==b){
                System.out.println("a and b is equal");
            }
            if (a==c){
                System.out.println("a and c is equal");
            }
            System.out.println("---结束if语句---");
            System.out.println();
    
            c = a < b ? a:b;
            System.out.println("三元运算符:"+ c);
    
            "========================================================"
            if(关系表达式) {
                语句体1;
            }else {
                语句体2;
            }
    
    //        if else语句
            System.out.println("---开始if...else语句---");
            if (a % 2 == 1){
                System.out.println("a是偶数");
            }else{
                System.out.println("a是奇数");
            }
            System.out.println("---结束if...else语句---");
            System.out.println();
            "========================================================"
                if (判断条件1) {
                    执行语句1;
                } else if (判断条件2) {
                    执行语句2;
                }
                ...
                }else if (判断条件n) {
                    执行语句n;
                } else {
                    执行语句n+1;
                }
    
    //        if...else if ...else
            int x = 5;
            int y;
            if (x >= 3) {
                y = 2 * x + 1;
            }else if (x >= -1 && x < 3){
                y = 2 * x;
            }else{
                y = 2 * x -1;
            }
            System.out.println("y值是:"+y);
            System.out.println();
    
            int score = 50;
            if (score<0 || score>100){
                System.out.println("your score is error!");
            }else if (score>=90 && score<=100){
                System.out.println("your score is excellent");
            }else if (score>=80 && score<90){
                System.out.println("your score is good");
            }else if (score>=70 && score<80){
                System.out.println("your score is fine");
            }else if (score>=60 && score<70){
                System.out.println("your score is pass");
            }else{
                System.out.println("your score is fail");
            }
    
        }
    
    }
    
    
    1. 选择语句:

      switch(表达式) {
          case 常量值1:
              语句体1;
              break;
          case 常量值2:
              语句体2;
              break;
          ...
          default:
              语句体n+1;
              break;
      }
      
      public class Select_Statements {
          public static void main(String[] args) {
      //        判断是星期几
              int weekday = 6;
              switch (weekday){
                  case 1:
                      System.out.println("Monday");
                      break;
                  case 2:
                      System.out.println("Tuesday");
                      break;
                  case 3:
                      System.out.println("Wednesday");
                      break;
                  case 4:
                      System.out.println("Thursday");
                      break;
                  case 5:
                      System.out.println("Friday");
                      break;
                  case 6:
                      System.out.println("Saturday");
                      break;
                  case 7:
                      System.out.println("Sunday");
                      break;
                  default:
                      System.out.println("your input info is error!");
                      break;
              }
              System.out.println();
      // 穿透性验证
              int i = 5;
              switch (i) {
                  case 0:
                      System.out.println("执行case0");
                      break;
                  case 5:
                      System.out.println("执行case5");
                  case 10:
                      System.out.println("执行case10");
                  default:
                      System.out.println("执行default");
              }
          }
      }
      
      1. switch比if else效率要高;
      2. switch中的表达式结果支持:byte,short,int,char;
  2. for循环和while的区别:

    1. for循环内置了breakcontinue语句,可以方便地控制循环的流程。break语句可以中断循环,提前退出;continue语句可以跳过当前迭代,进入下一次迭代
  3. java中参数的传递:

    1. 如果是基本数据类型则使用值传递,传递栈中的值
    2. 如果是引用数据类型则使用引用传递,传递栈中引用值地址
  4. 方法定义中的一些概念

    1. 形式参数:形参只有在被调用时才会被Java虚拟机分配内存空间,形参只有在方法内部有效。方法调用结束返回主调用方法后则不能再使用该形参变量;
    2. 实际参数:实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,它们都必须有确定的值,以便把这些值传送给形参
public class Method_Demo2 {
    public static void main(String[] args) {
        // 调用方法getSum,传递两个整数,这里传递的实际数据又称为实际参数
        // 并接收方法计算后的结果,返回值
        int sum = getSum(5, 6);
        System.out.println(sum);
}
/*
定义计算两个整数和的方法
返回值类型,计算结果是int
参数:不确定数据求和,定义int参数.参数又称为形式参数
*/
public static int getSum(int a, int b) {
    return a + b;
    }
}
  1. 方法重载:
    1. 重载的前提条件是在同一类中;方法名相关,形参类型不同相关,形参个数相关,形参类型顺序相关和返回值类型无关
    2. 返回值类型不同不构成重载;
public class Method_Demo3 {
    public static void main(String[] args) {
        //定义不同数据类型的变量
        byte a = 10;
        byte b = 20;
        short c = 10;
        short d = 20;
        int e = 10;
        int f = 10;
        long g = 10;
        long h = 20;
        System.out.println(compare(a, b));
        System.out.println(compare(c, d));
        System.out.println(compare(e, f));
        System.out.println(compare(g, h));
    }
    public static boolean compare(byte a, byte b){
        System.out.println("====byte=====");
        return a ==b;
    }

    public static boolean compare(short a, short b){
        System.out.println("====short====");
        return a ==b;
    }

    public static boolean compare(long a, long b){
        System.out.println("====long====");
        return a ==b;
    }

    public static boolean compare(int a, int b){
        System.out.println("====int====");
        return a ==b;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值