Java流程控制学习笔记3

switch多选择结构

  • 多选择结构还有一个实现方式就是switch case语句

  • switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支

  • switch语句中的变量类型可以是:

    • byte,short,int或者char

    • 从Java SE 7 开始

    • switch支持字符串String类型

    • 同时case标签必须为字符串常量或字面量。

     

     

    public class SwitchDemo01 {
       public static void main(String[] args) {
           //case穿透   // switch 匹配一个具体的值
           char grade = 'C';
           switch (grade){
               case 'A':
                   System.out.println("优秀");
                   break;//可选
               case 'B':
                   System.out.println("良好");
                   break;//可选
               case 'C':
                   System.out.println("及格");
                   break;//可选
               case 'D':
                   System.out.println("再接再厉");
                   break;//可选
               case 'E':
                   System.out.println("挂科");
                   break;//可选
               default:
                   System.out.println("未知等级");
          }
      }
    }
    public class SwitchDemo02 {
       public static void main(String[] args) {
           String name = "李四";
           //jDk7的的新特性 支持字符串
           //反编译java----class( 字节码文件)----反编译(IDEA)
           switch (name) {
               case "张三":
                   System.out.println("优秀");
                   break;//可选
               case "李四":
                   System.out.println("良好");
                   break;//可选
               default:
                   System.out.println("未知等级");
          }
      }
    }

     

循环结构

while循环

  • while是最基本的循环,结构:

while(布尔表达式){
   //循环结构
}
  • 只要布尔表达式为true,循环就会一直执行下去

  • 我们大多数情况是会让循环停止下来,我们需要一个让表达式失效的方式来结束循环

  • 少部分需要循环一直执行,比如服务器的请求响应监听等

  • 循环条件一直为true就会造成无限循环(死循环),正常编程应该避免,会影响程序性能或者程序卡死崩溃

public class WhileDemo01 {
   public static void main(String[] args) {
       //输出1~100
       int i = 0;
       while (i<100){
           i++;
           System.out.println(i);
      }
  }
}
public class WhileDemo02 {
   public static void main(String[] args) {
       //死循环
       while (true){
       //等待客户端连接
       //定时检查
      }
  }
}
public class WhileDemo03 {
   public static void main(String[] args) {
       //计算1+2+3+4...+100=?
       //
       int i = 0;
       int sum = 0;

       while (i<=100){
           sum = sum + i;
           i++;
      }
       System.out.println(sum);
  }
}

 

do...while循坏

  • 对于 while 语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。

  • do...while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。

    do {
    //代码语句
    }while(布尔表达式);
  • While和do-While的区别:

    • while先判断后执行。dowhile是先执行后判断!

    • Do…while总是保证循环体会被至少执行一次! 这是他们的主要差别。

public class DoWhileDemo01 {
   public static void main(String[] args) {
       int a = 0;
       while(a<0){
           //不进循环
           System.out.println(a);
           a++;
      }
       System.out.println("---------------");
       do{
           //循环里面走了一次
           System.out.println(a);
           a++;
      }while (a<0);
  }
}

 

posted @   lan9277  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
欢迎阅读『Java流程控制学习笔记3』
点击右上角即可分享
微信分享提示