Java check if number in interval(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检查间隔中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java检查间隔中的数字
基础教程推荐
- Struts2 URL 无法访问 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
