Java检查间隔中的数字

2022-01-24 00:00:00 intervals range java

可能重复:
Java 是否存在开放式区间实现?

我有一个 int 变量,我想检查它的值是否在区间 [a,b] 内.我知道使用 x>=a 和 x<=b 或实现一个可以做到这一点的简单方法是一件简单的事情,但我想知道是否已经完成了一些事情.在数学课上搜索,但我找不到.这不是那么重要,也不是那么大的问题,但我很好奇是否有这样的东西,所以我可以使用它而不是实现我自己的:)

i have an int variable and i'd like to check if it's value is in an interval [a,b]. I know it's a simple matter of using x>=a and x<=b or implementing a simple method which can do this, but i'd like to know if there is something already done. Searched in Math class, but i wasn't able to find one. It's not that important and not that big of an issue, but i'm curious if there is something like this, so i can use it instead of implementing my own :)

在我所有的编码中,我都没有遇到过这样的方法.也许你们中的一个java大师有:)

In all my coding i haven't come across a method like this. Maybe one of you java gurus have :)

谢谢.

推荐答案

使用这个:

public static boolean intervallContains(int low, int high, int n) {
    return n >= low && n <= high;
}

相关文章