java - 范围为int的switch语句 [英] java - switch statement with range of int
问题描述
我想使用switch语句检查一些数字,我发现有几个地方说像
case 1 ... 5
或 case(得分> = 120)&& (得分< = 125)
会起作用,但我只是不知何故继续犯错误。
I want to use a switch statement to check a range of numbers I have found a few places saying something like
case 1...5
or case (score >= 120) && (score <=125)
would work but I just somehow keep on getting errors.
如果数字介于1600之间,我想要的是-1699然后做点什么。
What I want is if the number is between 1600-1699 then do something.
我可以做if语句,但是如果可能的话,我觉得是时候开始使用switch了。
I can do if statements but figured it's time to start using switch if possible.
推荐答案
在JVM级别开关
语句与if语句根本不同。
On the JVM level switch
statement is fundamentally different from if statements.
Switch是关于必须在编译时指定的编译时常量,以便javac编译器生成有效的字节码。
Switch is about compile time constants that have to be all specified at compile time, so that javac compiler produces efficient bytecode.
在Java 开关
语句不支持范围。您必须指定所有值(您可能会利用掉落的情况)和默认
案例。如果语句,还必须处理其他任何事情。
In Java switch
statement does not support ranges. You have to specify all the values (you might take advantage of falling through the cases) and default
case. Anything else has to be handled by if
statements.
这篇关于java - 范围为int的switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!