Java程序流程控制

程序流程控制

第一章:程序流程控制

顺序结构(程序默认结构)

从上到下;

分支结构
  1. if (根据判定的结果(真假)决定执行某个分支的代码)(注意:适合做区间匹配)

    • 三种格式

      • //格式1:
        if (条件表达式) {
            语句体;
        }
        // 格式2:
        if (条件表达式) {
            语句体1} else {
            语句体2}
        // 格式3:
        if (条件表达式 1){
            语句体 1} else if (条件表达式 2) {
            语句体 2} else if ( 条件表达式 3){
            语句体 3}
        ...
            else {
                语句体n+1}
        
      • 案例:

        public class IfDemo1{
            public static void main(String[] args ) {
                // 需求: 心跳(60~120) 之间是正常的,否则系统提示进一步检查
                int hertBeat = 30;
                if(hertBeat < || hertBeat > 10) {
                    System.out.println("您的心跳数据是:" +heartBeat+"您需要进一步检查")}
                System.out.print("检查结束");
            }
        }
        // 格式2:
        // 需求:发红包
        double money = 5999// 发送一个1314
        if (money >= 1314){
          System.out.print("您当前发送红包成功");
          
          } else {
            System.out.println("没钱了请充值!");
          }
          //绩效系统 0-60 c 60-80 B 80-90 A 90-100 A+
         public static void main(String[] args) {
                // int score = 99;
                Scanner sc = new Scanner(System.in);
                int score = sc.nextInt();
                System.out.println("请输入宁本月的绩效:");
                if (score >= 0 && score < 60) {
                    System.out.print("您本月的绩效是:c");
                } else if (score >= 60 && score < 80) {
                    System.out.println("您本月的绩效为:B");
                } else if (score >= 80 && score < 90) {
                    System.out.println(" 您本月的绩效是:A");
        
                } else if (score >= 90 && score <= 100) {
                    System.out.println("太棒了!");
                }
            }
        }
        
  2. switch分支:匹配条件执行分支,适合做值匹配的分支选择,结构清晰,格式良好;

    • 先执行表达式的值,用这个值与case后的值进行匹配

    • 匹配那个case的值为true就执行那个case 遇到break就跳出switch

    • 若case后的值都不匹配则执行default代码;

    • 性能好,代码优雅;

    • 格式:

      switch(表达式) {
          case1:
              执行代码...;
              break;
          case2:
               执行代码...;
              break;
          default:
              执行代码n;
      }
      // 案例:
      public class SwitchDemo3 {
           public static void main(String[] args) {
               String weekday ="周三";
               switch(weekday) {
                   case "周一"System.out.print("卷起阿里");
                       break;
                       case"周二" :
                        System.out.print("再学习");
                       break;
                   default:
                       System.out.print("数据有误,重新输入");
               }
      }
      
    • 注意事项:

      • 表达式类型只能是byte、short、int、char,JDK5开始支持枚举,JDK7开始支持String、不支持double、float、long

      • case给出的值不允许重复,且只能是字面量,不能为变量。

      • 不要忘记写break,否则会出现穿透现象。

  3. switch的穿透性

    • 若代码执行到没有写break的case块,执行完后到下一个break跳出程序
    • 若存在多个case分支的功能代码是一样的,可以用穿透型流程集中到同一处处理,可以简化代码。
循环结构
  1. for 循环

    • 格式:

      for (初始化语句;循环条件;迭代语句) {
          循环体语句(重复执行的代码)
      }
      // 示  例: 
      for (int i = 0;i < 3; i++) {
          System.out.println("hello world")
      }
      
  2. for 循环案例

    // 求和
     
    // 需求:求1-5之间的数据和,并吧求和结果在控制台输出。
    public class SumDemo1 {
        public static void main(String[] args) {
            // 求1~5数据和
            int sum = 0;
            for (int i = 1; i <= 5; i++) { // 快捷键:5.fori 按tab
                //sum = sum + i;
                sum += i;
            }
            System.out.println(sum);
        }
    }
    // 求奇数和
    // 需求:求1-10之间的奇数和,并把求和结果在控制台输出
    public class SumDemo1 {
        public static void main(String[] args) {
            for (int i = 1; i <=10; i+=2) { // 1-10
                if (i%2== 1) {   //筛选出奇数
                    sum +=i;
                    
                }
            }
            // 4输出求和变量
            System.out.println(sum)
        }
    }
    // 案例3
    水仙花束
    // 需求在控制台输出所有的水仙花数 1. 水仙花数是一个三位数 2. 水仙花数,个位,十位百位的数字立方和等于原数
    public class ForTest4{
    public static void main(String[] args) {
              // 用于记录水仙花个数
              int count = 0;
              
              for(int i = 100; i<=999; i++) {
                 // 判断这个三位数是否满足要求
                 // i = 157
                 // 个位
                 int ge = i % 10;
                 //十位
                 int shi = i / 10 %10;
                 // baiwei 
                 int bai  = i /100;
                 // 、
                 if ((ge * ge *ge + shi* shi* shi+bai*bai*bai)== i)
                 Sysyem.out.print(i+"\t");
                 count++;
              }
              System.out.print(count);
              }
              }
            
    }
    
  3. while 循环

    • 格式

      while(循环条件) {
          循环语句 (被重复执行的代码);
              迭代语句;
      }
      // 例子:
      int i = 0;
      while (i<3) {
         System.out.println("Hello  Word ")
         i++; // 必须有,不然死循环
      }
      
    • 什么时候使用for循环,什么时候使用While循环

      • 知道循环几次:使用for
      • 不知循环几次:建议使用While
  4. while循环案例

    • 珠穆朗玛峰案例

    • //珠穆朗玛峰高度是8848860m纸张厚度0.1
      double  peakHeight = 8848860;
      double paperThickness = 0.1
          int count = 0;
          while ( paperThickness< peakHeight ) {
              paperThickness *=2;
              count++;
          }
      //输出count
      
  5. do-while 循环

    • 格式

      初始化语句;
      do {
         循环体语句;
         迭代语句;
      } while (循环条件)// 举例
      int i = 0;
      do {
          System.out.print("hello");
          i++;
      } while (i<3);
      
    • 特点:一定会先执行一次循环体

  6. 三种循环的区别

    • for循环和while循环(先判断后执行)
    • do…while(第一次先执行后判断)
    • 清楚循环次数建议用for循环,不清楚建议使用while循环
    • for循环有生命周期,即循环体
  7. 死循环

    • 写法

      //经典写法
      while(true) {
          // 内容
      }
      
      do {
          //内容
      } while (true);
      // for死循环
      for(;;) {
          // 内容
      }
      
    • 案例:

      public class Demo{
          public static void main(String[] args ){
              // 1 、定义正确的密码
              int okPwd = 520;
              // 1、定义一个死循环让用户不断输入密码认证
               Scanner sc = new Scanner(System.in);
              while (true) {
                  System.out.print("请输入正确的密码!")
                      int passd = sc.nextInt();
                  // if 判断密码是否正确
                  if (passd == okPwd){
                      System.out.println("登录成功")
                          break;
                  } else {
                      System.out.println("密码错误")}          
                  
              }
              
          }
      }
      
  8. 循环嵌套

    • 四行五列矩形

      for (int i = 0; i<4; i++) {
          for (int j = 0; j<5; j++){
              System.out.print("*");
          }
            System.out.print( );//换行
      }
      
跳转关键字
  1. break
  2. continue:跳出当前循环 的当次执行,进入下一次循环
案例技术: 随机数Random类
  1. Random的使用

    • 特点

      • nextlnt(n)功能只能生成:0-(n-1)之间的随机数

      • Random生成区间随机数的技巧:加减法

      • eg:生成1~10之间的随机数程序实现

        1-10 -> -1( 0 - 9 ) + 1

        Random r = new Random();

        int num = r.nextlnt(10) + 1;//1-10

        // 3 - 17 ==> -3 ==>(0 -14)+3
        r.nextInt(15)+3 // 15是0-14的区间15个数,我们要的结果是3到17个区间的数需要加三
        
      • 调出程序流程控制快捷键

        • CTRL+ALT+T
    • 猜数字游戏

      • package com.itheima.day06Method;
        
        import java.util.Random;
        import java.util.Scanner;
        
        public class RandomDemo1 {
        
            public static void main(String[] args) {
                Random r = new Random();
               int rs = r.nextInt(100)+1;
        //        System.out.println(rs);//Ctrl+alt+t
                Scanner sc = new Scanner(System.in);
        
                while (true) {
                  //让用户输入数据
                    System.out.println("亲输入猜测的数据(1-100)");
                  int rs1  = sc.nextInt();
        
                  //判断幸运号码的猜测情况
                    if (rs1 > rs) {
                        System.out.println("您猜测数据过大");
        
                    } else if (rs1 < rs) {
                        System.out.println("您猜测数据较小");
        
                    }else {
                        System.out.println("猜对了!");
                        break;
                    }
                }
        
            }
        }
        
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值