Java检查间隔中的数字 [英] Java check if number in interval

查看:34
本文介绍了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;
}

这篇关于Java检查间隔中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆