开发者

find number range in java

How to get number range in java? for instance how can verify is the number 2389 within 10 numbers from 2400. its not but 2389 is.

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.

In comes the next number 989 range is always 1000, I return false because the between 989 and开发者_JAVA技巧 1000 is 11 numbers diference.

In comes the next number 1013 range is always 1000, I return false because the between 1013 and 1000 is 13 numbers diference.


You can use absolute values and Math.abs(num1-num2) <= 10


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜