doujiao7679 2013-05-14 02:05
浏览 45
已采纳

在Java中切换案例逻辑表达式语句 - 与JS或PHP

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.

  • 写回答

2条回答 默认 最新

  • dsuw85815 2013-05-14 02:10
    关注

    No You cannot do that using switch in java. You have to use if-else to accomplish this.

    See this <kbd>link</kbd> for more details.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?