Given is an integer with the value 5. Given is a range with start and end values of 2 and 5. So is the integer 5 now between the range? What should a 'InBetween()' method return and which one is the correct representation of 'between':

int val = 5

if(val >= 2 && val <= 5) // would return true

if(val > 2 && val < 5) // would return false
有帮助吗?

解决方案

There is no clear answer to this as "between" can be interpreted in lots of way. I'd argue that inBetween(1, 4) should check 2, 3 but that's just me.

There are other examples like, Random.nextInt(1,4) - does it return values 1,2,3,4; 2,3 or 2,3,4?

So most functions tend to clarify that in the documentation, for instance:


public String substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. Examples:

 "hamburger".substring(4, 8) returns "urge"
 "smiles".substring(1, 5) returns "mile"

Parameters:

  beginIndex - the beginning index, inclusive.
  endIndex - the ending index, exclusive.

If you want to be precise, then use the common mathematical notation as described here:

enter image description here

So [1, 5) means the interval 1,2,3,4 (integers) or 1 to 4.999... if we're using floats.

其他提示

For integer types, it's normal practice to include the specified values within the range. You would say that the range of a single standard die is between 1 and 6, not 0 and 7.

For floating point types, it varies depending on the specification of the particular project. When defining adjacent bands, it's common to include one value, but to exclude the other.

e.g.

double value
if (value >= 0.0 && value < 2.0)
    ...
else if (value >= 2.0 && value < 5.0)
   ...

In math, there are four interval (range) specification forms, shown here with corresponding C expressions:

(a,b) ((a < x) && (x < b))
[a,b) ((a <= x) && (x < b))
(a,b] ((a < x) && (x <= b))
[a,b] ((a <= x) && (x <= b))

The idea is that ( and ) denote open ends, while [ and ] denote closed ends.

Added, based on the comment from "stakx": An open end of an interval is one that does not include the actual endpoint. A closed end is one that does include the endpoint. For example, (3,5) is an open interval, 3 < x < 5. Neither 3 nor 5 are included. [4,6) is a half-open interval: 4 is included, 6 is not. [5,7] is a closed interval: both 5 and 7 are included.

The different forms are not that interesting for integers, as any open or half-open integer interval can be expressed as the equivalent closed interval (and vice versa, of course). They become useful for rationals and reals.

Saying "in between" is ambiguous. It doesn't say whether the endpoints are included (i.e., is the range open or closed on either or both ends). You need to specify it more clearly, so you can say which of the four forms is intended as the specification, given the endpoints.

许可以下: CC-BY-SA归因
scroll top