Home > Back-end >  Loop completely skips else
Loop completely skips else

Time:02-05

I created this program to check every three elements in an array to see which out of the three is the lowest number. I am having problems with the third checking of the number. It seems that my program completely skips the else statement and I do not know why. Please help.

public class MinimumThree{
    
    public static void main(String[] args){
        int[] allNums = {9546090, -2970855, -9855973, //0
9371931, 6384382, -6572105, //1
8010119, 4650629, 1809075,  //2
621406, -533910, -6641563,  //3
2239374, 9103245, 9449092,
2151455, 8992428, -2024049, //5
-974953, -8609904, -1604141,//6
-6093141, 694404, 6682392,  
-2768011, -2470814, 3542642,
-4554484, 6261148, 5609316,
7640387, -2091321, -9378998,  //10
-1964472, -1923829, 1892163,
-8099458, 9384232, -9611172,    //12
-9231042, -1957104, -769289,
-8614838, -5320162, -3788791,
8874853, -5623631, 2014317, //
5167069, -6186510, -3315506,//
9591483, 9139903, 295243,//
-2116652, -9305470, 6707044,//
-980294, 6488409, -744023,
-4367004, -2692417, -2723892};
        
        int num1, num2, num3;
        int[]minNums = new int[21];
        int counter = 0;
        int i;
        //TEST
        System.out.println("AllNums length: "   allNums.length);
        //-----
        
        for( i =2; i < allNums.length; i =3){
            num1 = allNums[i];   
            num2 = allNums[i-1]; 
            num3 = allNums[i-2]; 
            
            if(num1 < num2){
                if(num1 <num3){
                    minNums[counter] = num1;
                    //TEST
                    System.out.println("Counter before: "   counter);
                    //System.out.print(minNums[counter]   " ");
                    //counter  ;
                    //TEST
                    System.out.println("Counter after: "   counter);
                }
            }
            else if(num2 < num1){
                if(num2 < num3){
                    minNums[counter] = num2;
                    //TEST
                    System.out.println("Counter before: "   counter);
                    //System.out.print(minNums[counter]   " ");
                    //counter  ;
                    System.out.println("Counter after: "   counter);
                }
            }else{
                //TEST TEST TEST
                System.out.println();
                System.out.println("NUM3: "   num3);
                //
                minNums[counter] = num3;
                //TEST
                System.out.println("Counter before: "   counter);
                //System.out.print(minNums[counter]   " ");
                //counter  ;
                //TEST
                System.out.println("Counter after: "   counter);
            }
            counter  ;
        }
        //TEST
        System.out.println();
        System.out.println("i end result: "   i);
        System.out.println("counter end result: "   counter);
        //--------
        for(int j = 0; j < minNums.length; j  ){
            System.out.print(minNums[j]   " ");
        }
        //i = 2;  i  =3;  i = 5
        //array[i]
        //a = i - 2;
        //b = i - 1;
        //c = array[i]
    }
}

Console Output

I tried putting the incremented counter outside the if statements to see if that was the error, but it was not.

CodePudding user response:

Your else if statements aren't formatted correctly. Currently, unless num1 == num2 they will never reach the last else statement, as num1 or num2 is usually bigger than the other. Your else ifs need to look something like

if (num1 < num2 && num1 < num3) {
    ...
  } 
else if (num2 < num1 && num2 < num3) {
     ...
  } 
else {
     ...
}
  • Related