Java check if number in interval [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-06 03:58:58

问题


Possible Duplicate:
Does an open-ended interval implementation exist for Java?

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 :)

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

Thanks.


回答1:


Use this:

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



回答2:


I'd rather go for writing this method myself than including a third-party dependency :)



来源:https://stackoverflow.com/questions/9532254/java-check-if-number-in-interval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!