如何检查一个整数是否在给定范围内?
希望有比这更优雅的东西
if (i>0 && i<100)
Hoping for something more elegant than
if (i>0 && i<100)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
希望有比这更优雅的东西
if (i>0 && i<100)
Hoping for something more elegant than
if (i>0 && i<100)
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(22)
您可以添加间距;)
You could add spacing ;)
对于那些使用 commons lang 的人来说,一个选项是使用 范围:
另请参阅:如何构建 apache commons 3.1 Range;对象
For those using commons lang an option is to use Range:
Also see: how to construct a apache commons 3.1 Range<Integer> object
我认为
更优雅。看起来像数学方程。
如果您正在寻找一些特别的东西,您可以尝试:
至少它使用了库。
I think
is more elegant. Looks like maths equation.
If you are looking for something special you can try:
at least it uses library.
ValueRange
我们可以重新调整
java.time.temporal.ValueRange
类位于 Java 8+ 内置的 java.time 框架中。此代码:
minValue <= x <= MaxValue
,则返回true
- 即在范围内x
x <= MaxValue,则返回
或false
minValuex > maxValue
- 即超出范围与
if
条件一起使用,如下所示:以下程序检查
hasTeen
方法中传递的任何整数值是否在范围内范围为 13(包含)到 19(包含)。输出
ValueRange
We can repurpose the
java.time.temporal.ValueRange
class found within the java.time framework built into Java 8+.This code:
true
ifminValue <= x <= MaxValue
- i.e. within the rangefalse
ifx < minValue
orx > maxValue
- i.e. out of rangeUse with
if
condition as shown below:The below program checks, if any of the passed integer value in the
hasTeen
method is within the range of 13 (inclusive) to 19 (inclusive).OUTPUT
我认为这已经是比较范围的优雅方式。但是,这种方法会导致您编写额外的单元测试来满足所有
&&
情况。因此,您可以使用以下任何方法来避免编写额外的单元测试。
使用 Java 8 Streams:
使用 Math 类:
我个人推荐第二种方法,因为它最终不会创建一个大小等于您要检查的范围的数组。
I think its already elegant way for comparing range. But, this approach cause you to write extra unit tests to satisfy all
&&
cases.So, you can use any of the below approach to avoid writing extra unit tests.
Using Java 8 Streams:
Using Math class:
Personally I recommend the second approach because it won't end up creating an Array of the size equal to the range you want to check.
我不认为这不优雅,但如果您经常重复该表达式,那么最好将其放入方法中,例如,
如果您混合独占比较和包含比较,则尤其如此。方法名称可以帮助避免拼写错误,例如使用 <当 <= 应该被使用时。该方法还可以确保min<0。最大等..
I don't see how that's not elegant, but if you repeat the expression often, then it's a good idea to put it into a method, e.g.
This is particularly true if you mix exclusive and inclusive comparisons. The method name can help avoid typos, such as using < when <= should have been used. The method can also take care of ensuring that min < max etc..
如果您正在寻找比
您更原创的东西,可以尝试这个
If you're looking for something more original than
you can try this
这家伙做了一个不错的< href="https://headcrashing.wordpress.com/2011/01/01/range-class/" rel="nofollow noreferrer">范围 类。
然而,它的使用不会产生好的代码,因为它是一个通用类。你必须输入类似这样的内容:
or (如果你先实现的话会更好一些):
恕我直言,语义上两者都比 Java 默认提供的更好,但是内存开销、性能下降和更多的打字总体上是值得的。就我个人而言,我更喜欢 mdma 的方法。
This guy made a nice Range class.
Its use however will not yield nice code as it's a generic class. You'd have to type something like:
or (somewhat better if you implement first):
Semantically both are IMHO nicer than what Java offers by default, but the memory overhead, performance degradation and more typing overall are hadly worth it. Personally, I like mdma's approach better.
Google 的 Java 库 Guava 还实现了 Range< /a>:
Google's Java Library Guava also implements Range:
使用此代码:
Use this code :
只是我的 2 cts
称它为 1 到 99 之一
Just my 2 cts
call it like to see it is one of 1 to 99
如果您使用 Spring 数据,您还可以使用 Spring 中的 Range 对象。
if you are using Spring data you can also use the Range object from Spring.
这就是检查整数是否在范围内的方法。大于下限,小于上限。试图巧妙地进行减法可能不会达到您想要的效果。
That's how you check is an integer is in a range. Greater than the lower bound, less than the upper bound. Trying to be clever with subtraction will likely not do what you want.
尝试一下:
至少会起作用;)
Try:
it will work at least ;)
如果您只是想测试
i
的值是否在 [0..100) 范围内,并且想要布尔结果,那么就可以,并且会给您
true
或假
。如果您愿意在值超出范围时抛出异常,您可以使用内置的 checkIndex 方法,如果超出范围,将抛出
IndexOutOfBoundsException
。诚然,这不是一个通用的解决方案,并且只有在您的上下文是为检查数组边界而进行范围检查的上下文中才真正更漂亮,但它的优点是不需要任何第三方库,这对于小型程序。If you just want to test whether the value of
i
is in the range [0..100), and you want the boolean result, thenis fine and will give you
true
orfalse
. If you are willing to throw an exception if the value is out of range, you can use the built-in checkIndex methodwhich will throw
IndexOutOfBoundsException
if out of range. Granted, it is not a general solution, and is really only prettier if your context is one in which your range check is being done for checking array bounds, but it has the advantage of not needed any third party libraries, which is nice for small programs.试试这个如果策略。看起来很扎实。
Try this if strategy. Seems solid.
编写此程序是为了根据范围检查 3 个整数参数的值。
我创建了一个单独的方法来使用 if/else-if 语句检查每个值的范围。
我希望这对某人有帮助。
Wrote this program to check the value of 3 integer parameters against a range.
I have created a separate method to check the range of the each value using if/else-if statements.
I hope this helps someone.
好吧,这是一个很老的问题,有很多解决方案。 另一个(您可以决定这是否更优雅)
这是OP示例中的
:但我认为这与使用库的任何其他解决方案一样庞大。
Ok, this is a quite old question, and there are many solutions. Here another one (you may decide if this is more elegant or not)
in the example of the OP:
But I think this is so bulky like any other solution using a library.
if(i <= 0 || i >=100)
它将起作用。
if(i <= 0 || i >=100)
It will work.
如果您确信数字以 2 的补码形式存储:
更通用、更安全的解决方案是:
If you are confident that numbers are stored in 2's complement form:
A more general and safer solution would be: