我正在尝试根据旧 Switch 案例的情况为变量赋值,它不允许有什么方法或者我应该在每个案例中编写初始化变量吗?
public static void printNumberInWord(int number){
String inWord = switch(number){
case 0 -> "ZERO";
case 1: "ONE";
break;
case 2: "TWO";
break;
case 3: "THREE";
case 4: "FOUR";
case 5: "FIVE";
case 6: "SIX";
case 7: "SEVEN";
case 8: "EIGHT";
case 9: "NINE";
default : "no Value"
};
增强型开关正在工作,如Case0
所示请参阅此链接,它通过示例说明了如何定义 switch 语句:
https://www.freecodecamp.org/news/java-switch-statement-how-to-use-a-switch-case-in-java/