1.switch语句
switch(choice){case 1->...case 2->...case 3->...case 4->...default->System.out.println("Bad input");
}
case的类型 char byte short int 枚举enum 字符串
多个变量可用,分隔
例:
String input=...;
switch(input.toLowerCase()){case "yes","y"->...case "no","n"->...default->...
}
yield关键字可以终结语句
case "Spring" ->{System.out.println("spring time!");yield 6;
}//返回值为6
相当于
case "Summer","Winter"->6;
int numLetters=switch(seasonName){case "Spring" ->{System.out.println("spring time");yield 6;}case "Summer","Winter" ->6;case "Fall" ->4;default -> -1;
};
这种情况没有break和yield语句,会根据seasonName执行
int numLetters=switch(seasonName){case "Spring":System.out.println("spring time");case "Summer","Winter": yield 6;case "Fall":yield 4;default:yield -1;
};
有yield语句