- Groovy 教程
- Groovy - 主页
- Groovy - 概述
- Groovy - 环境
- Groovy - 基本语法
- Groovy - 数据类型
- Groovy - 变量
- Groovy - 运算符
- Groovy - 循环
- Groovy - 决策制定
- Groovy - 方法
- Groovy - 文件 I/O
- Groovy - 可选参数
- Groovy - 数字
- Groovy - 字符串
- Groovy - 范围
- Groovy - 列表
- Groovy - 映射
- Groovy - 日期和时间
- Groovy - 正则表达式
- Groovy - 异常处理
- Groovy - 面向对象
- Groovy - 泛型
- Groovy - 特征
- Groovy - 闭包
- Groovy - 注释
- Groovy - XML
- Groovy - JMX
- Groovy - JSON
- Groovy - DSLS
- Groovy - 数据库
- Groovy - 生成器
- Groovy - 命令行
- Groovy - 单元测试
- Groovy - 模板引擎
- Groovy - 元对象编程
- Groovy 有用资源
- Groovy - 快速指南
- Groovy - 有用资源
- Groovy - 讨论
Groovy - 复杂 switch 语句
还可以有成套的 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,值是 2。然后我们使用一个 switch 语句,它对变量 a 的值进行求值。它会根据变量的值执行相关的 case 语句。上述代码的输出应为:-
i is 0, j is 1
groovy_decision_making.htm
广告