Java中switch的三种⽤法⽅式Java14
从java14开始, switch语句有了⼀个很⼤的调整, 这就让swicth语句有了更多的操作和选择,在代码上,更加的简便灵活.
switch 标准⽅式
switch - > ⽤法:
switch yield ⽤法:
标准模式
int flag =3;
switch(flag){
case0: System.out.println("The number is 0");break;
case1: System.out.println("The number is 1");break;
case2: System.out.println("The number is 2");break;
default: System.out.println("you are right");break;
}
switch - > ⽤法:
使⽤ - > ⽅法更加简单, 就不⽤使⽤ break 命令, 保证只有⼀种路径会被执⾏!
int flag =3;
switch(flag){
case0-> System.out.println("The number is 0");
case1-> System.out.println("The number is 1");
case2-> System.out.println("The number is 2");
default-> System.out.println("you are right");
}
switch yield ⽤法:返回值
int flag =2;
String ss;
ss =switch(flag){
case0->"The number is 0";
case1->"The number is 1";
java switch case string
case2->{
String c ="The number is 2";
yield c;
}
default->"you are right";
};
System.out.println(ss);
输出

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。