0

Is it possible to limit the range of a number as in for example: x to be between 0 and 24,if x drops to -1 he would instead become 24,-2 would be 23 and the same with 25 - > 0 ,26 -> 1.

  • 1
    You can do that manually, with a custom class (maybe extending `Number` too). – Mena Nov 29 '17 at 16:02
  • 1
    check out this question https://stackoverflow.com/questions/17933493/java-limit-number-between-min-and-max – wleao Nov 29 '17 at 16:03

1 Answers1

1

Use somehting like

  class BoundedInteger {

private static final int MAX_VALUE = 25;
private int value;

BoundedInteger(int value) {
    value %= MAX_VALUE;
    if (value < 0)
        value = MAX_VALUE - Math.abs(value);
    this.value = value;
}

public int getValue() {
    return value;
}

public void setValue(int value) {
    this.value = value % MAX_VALUE;
  }
}
Mustapha Belmokhtar
  • 1,231
  • 8
  • 21