dart switch 语句 多个switch嵌套是跳出指定switch
在 Dart 中,可以使用 break 语句来跳出 switch 语句。如果有多个嵌套的 switch 语句,可以在 break 语句后面添加一个标签,指定要跳出的 switch 语句。
例如,下面的代码演示了如何使用标签来跳出指定的 switch 语句:
void main() {
int x = 2;
int y = 3;
outerSwitch: switch (x) {
case 1:
print('x is 1');
innerSwitch: switch (y) {
case 2:
print('y is 2');
break outerSwitch; // 跳出外部的 switch 语句
case 3:
print('y is 3');
break innerSwitch; // 跳出内部的 switch 语句
}
break;
case 2:
print('x is 2');
break;
default:
print('x is neither 1 nor 2');
break;
}
}
在上面的代码中,我们使用了 outerSwitch 和 innerSwitch 标签来标记外部和内部的 switch 语句。在内部的 switch 语句中,当 y 的值为 2 时,我们使用 break outerSwitch; 语句来跳出外部的 switch 语句。当 y 的值为 3 时,我们使用 break innerSwitch; 语句来跳出内部的 switch 语句。这样就可以根据需要跳出指定的 switch 语句了
原文地址: https://www.cveoy.top/t/topic/dUcH 著作权归作者所有。请勿转载和采集!