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检查间隔中的数字


基础教程推荐
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01