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


基础教程推荐
- Java Swing计时器未清除 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01