I want to return the number as long as it falls within a limit, else return the maximum or minimum value of the limit. I can do this with a combination of Math.min
The Math.max(int a, int b)
function is defined as:
public static int min(int a, int b) {
return (a <= b) ? a : b;
}
So you can make a combination of the max
and min
functions as follows:
private static int MAX=10;
private static int MIN=0;
public int limit(int a) {
return (a > MAX) ? MAX : (a < MIN ? MIN: a );
}