在Java - 与JS或PHP中切换案例逻辑表达式语句 [英] Switch case logical expression statement in Java - versus JS or PHP
本文介绍了在Java - 与JS或PHP中切换案例逻辑表达式语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
switch(true){
case(d< 10):
document.write(Less than 10);
break;
case(d == 10):
document.write(等于10);
break;
case(d> 10):
document.write(大于10);
break;
默认值:
document.write(一些愚蠢的错误,可能不是一个数字。);
}
我想知道我们是否可以在Java中这样做...说我需要一个比较非常大的范围。
案例1:
案例2:
...
案例5000:
case 5001:
默认值:
这样做太麻烦了。我可以将我的案例包含在if语句中,但我想知道它可以像JavaScript或PHP一样完成。
解决方案
否你不能在java中使用switch你必须使用 if-else
来实现这一点。
看到这个 链接 了解更多详情。
in JavaScript and also I've seen it in PHP : You can use a logical expression inside you cases : For Example :
switch(true){
case (d<10):
document.write("Less than 10");
break;
case (d==10):
document.write("Equal to 10");
break;
case (d>10):
document.write("Greater than 10");
break;
default:
document.write("Some dumb error. Probably not a number.");
}
I was wondering if we can do this in Java ... say I needed a to compare a very large range.
case 1 :
case 2 :
...
case 5000:
case 5001:
default:
It would be tedious to do. I could wrap my cases inside if statements but i was wondering it could be done like in JavaScript or PHP.
解决方案
No You cannot do that using switch in java. You have to use if-else
to accomplish this.
See this link for more details.
这篇关于在Java - 与JS或PHP中切换案例逻辑表达式语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文