0

I am learning Java and I do not know what makes my code not reading my else-if condition (i == 5)

int i;    
for (i = 1; i <= 5; i++) {
    int guess = scanner.nextInt();
    System.out.println("Attempt: " + i +"/5");
    if (guess < numberToGuess) {
        System.out.println("Higher");
    } else if (guess > numberToGuess) {
            System.out.println("Lower");
    } else if (i == 5) {
        System.out.println("You failed! The correct answer is " + numberToGuess);
    } else {
        System.out.println("You won! Your answer is " + numberToGuess + " which is correct!");
        break;
    }

when i'm running the code and try to fail the game by reaching the maximum number of int i which is 5, the second else if statement should appear but it is not working.

I just don't get the reason why it is not reading the (i==5) condition, all of the other conditions are working except that.

azro
  • 53,056
  • 7
  • 34
  • 70

4 Answers4

0

Well, if-else statements work like this: when any one of the statements are satisfied, the rest of them are ignored. Take yours as an example.

if (guess < numberToGuess) {
    System.out.println("Higher");
} else if (guess > numberToGuess) {
    System.out.println("Lower");
} else if (i == 5) {
    System.out.println("You failed! The correct answer is " + numberToGuess);
} else {
    System.out.println("You won! Your answer is " + numberToGuess + " which is correct!");
    break;
}

Even when i == 5, if either of guess < numberToGuess or guess > numberToGuess are satisfied, the rest of the statements including (i == 5) are ignored.

To fix this, simply add i == 5 as the first condition to check, because if i == 5 is satisfied we shouldn't be considering the guess anyway.

I'm also guessing you want to break out of the loop if the game ends, so I added a break statement as well.

if (i == 5) {
    System.out.println("You failed! The correct answer is " + numberToGuess);
    break;
} else if (guess < numberToGuess) {
    System.out.println("Higher");
} else if (guess > numberToGuess) {
    System.out.println("Lower");
}  else {
    System.out.println("You won! Your answer is " + numberToGuess + " which is correct!");
    break;
}
Ryan Zhang
  • 1,856
  • 9
  • 19
0

So, that appears to be so, because before checking if you are under the right loop condition, there are checks of variables

... 
if (guess < numberToGuess) {
            System.out.println("Higher");
        } else if (guess > numberToGuess) {
                System.out.println("Lower");
        } 
....

Which are executed. So, to fix the bug, just move your check i == 5 to be the first branching clause. The code should look like next

if (i == 5) {
    System.out.println("You failed! The correct answer is " + numberToGuess);
} else if (guess > numberToGuess) {
    System.out.println("Lower");
} else if (guess < numberToGuess) {
     System.out.println("Higher");
} else {
    System.out.println("You won! Your answer is " + numberToGuess + " which is correct!");
    break;
}

This way you check the loop condition first and input condition after

kboskin
  • 422
  • 3
  • 12
0

That is how if/else if/else works, your value will be either lower or higher than the guess, so one for the 2 first condition will be true, then no other will be checked

Set the stop condition at first

int nb_tries = 5;
for (i = 1; i <= nb_tries; i++) {
    System.out.print("Attempt: " + i + "/" + nb_tries + " => ");
    int guess = scanner.nextInt();

    if (i == nb_tries) {
        System.out.println("You failed! The correct answer is " + numberToGuess);
    } else if (guess < numberToGuess) {
        System.out.println("Higher");
    } else if (guess > numberToGuess) {
        System.out.println("Lower");
    } else {
        System.out.println("You won! Your answer is " + numberToGuess + " which is correct!");
        break;
    }
}
azro
  • 53,056
  • 7
  • 34
  • 70
0

The Thumb rule while using if-else statement is which ever condition will satisfied to the earliest, it will go into that statement and after that it will execute the code outside of if-else condition.

So in your case Well, if-else statements work like this: when any one of the statements are satisfied, the rest of them are ignored. Take yours as an example.

if (guess < numberToGuess) {
     System.out.println("Higher"); } else if (guess > numberToGuess) {
     System.out.println("Lower"); } else if (i == 5) {
     System.out.println("You failed! The correct answer is " + numberToGuess); } else {
     System.out.println("You won! Your answer is " + numberToGuess + " which is correct!");
    break; }

Even when i == 5, if either of guess < numberToGuess or guess > numberToGuess is satisfied, it will execute either of these instead of i==5. So just think of the logic that you want to give more priority.