×

Groovy 教程

Groovy 概述Groovy 环境Groovy 基本语法Groovy 数据类型Groovy 变量Groovy 运算符Groovy 循环Groovy 条件语句Groovy 方法Groovy 文件I/OGroovy 可选Groovy 数字Groovy 字符串Groovy 范围Groovy 列表Groovy 映射Groovy 日期和时间Groovy 正则表达式Groovy 异常处理Groovy 面向对象Groovy 泛型Groovy 特征Groovy 闭包Groovy 注释Groovy XMLGroovy JMXGroovy JSONGroovy DSLSGroovy 数据库Groovy 构建器Groovy 命令行Groovy 单元测试Groovy 模板引擎Groovy 元对象编程

Groovy 嵌套Switch语句


Groovy 条件语句Groovy 条件语句


它也可以有一个嵌套的 switch 语句。语句的一般形式如下所示 -

switch(expression) { 
   case expression #1: 
   statement #1 
   ... 
   case expression #2: 
   statement #2
   ... 
   case expression #N: 
   statement #N 
   ... 
   default: 
   statement #Default 
   ... 
}

下面是嵌套switch语句的一个示例 -

class Example { 
   static void main(String[] args) { 
      //Initializing 2 variables i and j 
      int i = 0; 
      int j = 1; 
		
      // First evaluating the value of variable i 
      switch(i) { 
         case 0: 
            // Next evaluating the value of variable j 
            switch(j) { 
               case 0: 
                  println("i is 0, j is 0"); 
                  break; 
               case 1: 
                  println("i is 0, j is 1"); 
                  break; 
               
               // The default condition for the inner switch statement 
               default: 
               println("nested default case!!"); 
            } 
         break; 
			
         // The default condition for the outer switch statement 
         default: 
            println("No matching case found!!"); 
      }
   }
}

在上面的例子中,我们首先将a的变量初始化为a的值为2.然后我们有一个 switch 语句,它计算变量 a 的值。 基于变量的值,它将执行语句的相关案例集。上面的代码的输出将是 -

i is 0, j is 1

Groovy 条件语句Groovy 条件语句


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)