简体   繁体   English

在java中找到数字范围

[英]find number range in java

How to get number range in java? 如何在java中获取数字范围? for instance how can verify is the number 2389 within 10 numbers from 2400. its not but 2389 is. 例如,如何验证2389中的10个数字中的2389是数字。不是2389。

Ok here is the rephrase : 好的,这是措辞:

Number 1000 is the range number 990 is comming in the loop, I return true because the between 990 and 1000 is 10 numbers diference. 数字1000是循环中出现的范围数字990,我返回true,因为990和1000之间是10个数字差。

In comes the next number 989 range is always 1000, I return false because the between 989 and 1000 is 11 numbers diference. 在下一个数字989范围始终为1000时,我返回false,因为989和1000之间的数字为11。

In comes the next number 1013 range is always 1000, I return false because the between 1013 and 1000 is 13 numbers diference. 在下一个数字1013范围内始终为1000,我返回false,因为1013和1000之间的数字为13。

您可以使用绝对值和Math.abs(num1-num2) <= 10

public boolean isWithinRange(int number, int around, int range){
    int difference = Math.abs(around - number);
    return difference <= range;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM