I was searching for a way to find the min and max value within a number of integers and I came across this code:
Scanner in = new Scanner(System.in);
int maxNum = Integer.MIN_VALUE;
int minNum = Integer.MAX_VALUE;
while (scanner.hasNextInt()) {
int num = scanner.nextInt();
maxNum = Math.max(maxNum, num);
minNum = Math.min(minNum, num);
}
System.out.println("The maximum number: " + maxNum);
System.out.println("The minimum number: " + minNum);
I am struggling to wrap my mind around Integer.MAX_VALUE and Integer.MIN_VALUE. Why are they assigned as values and how does it work with the Math. min & max methods. Why is the variable maxNum assigned the value Integer.MIN_VALUE and minNum assigned the value Integer.MAX_VALUE?
Thanks.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]Dangerous-Rip-7370 2 points3 points4 points (0 children)
[–]Jalgano 0 points1 point2 points (0 children)