In the official code given by codegym, the max variable is assigned with the min value for an integer. But I don't understand why ?
int max = Integer.MIN_VALUE; //max variable being intialized as the min value of integer
while (console.hasNextInt()) {
int x = console.nextInt();
if (x % 2 == 0 && x > max) {
max = x;
}
}
In my code below, I assign the value 0, to the variable, maxNumber but not the min value for an integer. Thats why I don't fulfill the fifth requirement but I still don't understand. Can someone explain to me why the maxNumber variable should be assigned with min value for an integer ?package en.codegym.task.pro.task04.task0408;
import java.util.Scanner;
/*
Maximum of entered numbers
*/
public class Solution {
public static void main(String[] args) {
//write your code here
Scanner keyboardInput = new Scanner(System.in);
int maxNumber = 0;
while (keyboardInput.hasNextInt()) {
if (keyboardInput.nextInt() % 2 == 0) {
if (keyboardInput.nextInt() > maxNumber) {
maxNumber = keyboardInput.nextInt();
}
}
}
System.out.print(maxNumber);
}
}