[英]How to change if-else statement to switch case statement in java
My question is how to change if-else statement to switch case statement, I've done some work and it contain error in netbean:我的问题是如何将 if-else 语句更改为 switch case 语句,我已经做了一些工作,它在 netbean 中包含错误:
if(result.length <= 2){
if ((result[0].equals("UC"))||(result[0].equals("uc"))){
fix = "UC";
}
else if ((result[0].equals("LC"))||(result[0].equals("lc"))){
fix = "LC";
}
else
fix = "error";
}else
fix = "length";
Try like this像这样尝试
switch (result[0]) {
case "UC":
case "uc":
fix = "UC";
break;
case "LC":
case "lc":
fix = "LC";
break;
default:
fix = "error";
}
if(result.length <= 2){
switch(result[0]){
case "UC":
case "uc":
fix = "UC";
break;
case "LC":
case "lc":
fix = "LC";
break;
default:
fix = "error";
break;
}
}
else
fix = "length";
Another solution:另一种解决方案:
switch (Boolean.toString(result.length <= 2)) {
case "true":
switch (result[0]) {
case "UC":
case "uc":
fix = "UC";
break;
case "LC":
case "lc":
fix = "LC";
break;
default:
fix = "error";
}
break;
default:
fix = "length";
}
You can use the following:您可以使用以下内容:
if(result.length <= 2)
{
switch(result[0])
{
case "UC":
fix = "UC";
break;
case "uc":
fix = "uc";
break;
case "UC":
fix = "LC";
break;
case "uc":
fix = "lc";
break;
default:
fix = "error";
}
}
else
{
fix = "length";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.