Scott
Scott

Reputation: 319

"if (int == 1)" doesn't work

So I am trying to learn Java, and have been writing a basic program based off of the 21 matches game; in which each player takes turns taking 1,2 or 3 matches. The player to take the last match loses.

It was going well until I found that when the user was about to win, the program wouldn't enter the final else if statement; it would skip over it and come back to the user, without the computer having taken any matches. Attached is the method I have written for the computers turn.

public static int ComputerPlays(int matches){ //Method for the computers turn
    int matchesTaken = 0;

    if (matches > 7){ matchesTaken = new Random().nextInt(3)+1; }
    else if (matches > 4 && matches < 8){ matchesTaken = matches - 4; }
    else if (matches < 5){ matchesTaken = matches - 1; }
    else if (matches == 1){ matchesTaken = 1; }

    System.out.println("Computer takes " + matchesTaken + " matches");
    return(matches - matchesTaken);
}

The line else if (matches == 1){ matchesTaken = 1; } is where I'm having a problem. Any help would be appreciated.

P.S. I realize that I could turn the else if into an else statement, but the main thing is I want to learn what the problem is, not just get around it.

Upvotes: 1

Views: 2174

Answers (9)

Vrishank
Vrishank

Reputation: 364

You should move the (matches == 1) before (matches < 5) condition. The code is not reachable till (matches == 1) as this condition is satisfied by (matches < 5) also.

Updated code:

public static int ComputerPlays(int matches){ //Method for the computers turn
    int matchesTaken = 0;

    if (matches > 7){ matchesTaken = new Random().nextInt(3)+1; }
    else if (matches > 4 && matches < 8){ matchesTaken = matches - 4; }
    else if (matches == 1){ matchesTaken = 1; }
    else if (matches < 5){ matchesTaken = matches - 1; }

    System.out.println("Computer takes " + matchesTaken + " matches");
    return(matches - matchesTaken);
}

Upvotes: 2

Lee
Lee

Reputation: 241

1 is less than 5. This means that when matches is equal to one, it will always be captured in the statement "else if (matches < 5){ matchesTaken = matches - 1; }".

This is the reason why (matches == 1) is never executed.

Upvotes: 0

Shiladittya Chakraborty
Shiladittya Chakraborty

Reputation: 4418

if (matches > 7){ matchesTaken = new Random().nextInt(3)+1; }
else if (matches > 4 && matches < 8){ matchesTaken = matches - 4; }
else if (matches < 5){ matchesTaken = matches - 1; }
else if (matches == 1){ matchesTaken = 1; }

In your code first execute else if (matches < 5){ matchesTaken = matches - 1; }

This line execute first and this line also satisfy if matches == 1, thats why it will never goes to the next line (else if (matches == 1){ matchesTaken = 1; }) when matches == 1

Upvotes: 0

Thirumal
Thirumal

Reputation: 9686

When ever you use else if, use equal conditions (==) first then go for greater than or equal (X>=Y) or less than or equal(X<=Y) conditions.

The statement which satisfies the condition first will execute and rest of else if statements will ignore.

Happy Coding!

Upvotes: 0

Shree Krishna
Shree Krishna

Reputation: 8562

You've already checked else if (matches < 5) then it's useless checking
else if (matches == 1), because above condition will end the if chain. So you can change else if (matches < 5) to else if (matches < 5 && matches !=1) to make the last statement also reachable.

Upvotes: 0

Ani Menon
Ani Menon

Reputation: 28277

Put

else if (matches == 1){ matchesTaken = 1; }

before

else if (matches < 5){ matchesTaken = matches - 1; }

The order is important. In IF_ELSE, once a match is occurs it doesn't check the next condition. In your case as matches<5 is true it won't check matches==1.

Upvotes: 0

nhouser9
nhouser9

Reputation: 6780

Your problem is that the statement

else if (matches < 5){ matchesTaken = matches - 1; }

will be true when the matches is 1. Thus the else if will never be entered. You can change it to

else if (matches < 5 && matches != 1){ matchesTaken = matches - 1; }

That way, it will not trigger when the number of matches is 1 and the following block can execute successfully.

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172628

The condition (matches == 1) will be unreachable since your previous condtion is always makes your later condition true ie., if (matches < 5) which actually considers matches == 1. You can make the last two condition as:

else if (matches < 5 && matches != 1){ matchesTaken = matches - 1; }
    else if (matches == 1){ matchesTaken = 1; }

or like

else if (matches < 5 && matches > 1){ matchesTaken = matches - 1; }
    else if (matches == 1){ matchesTaken = 1; }

Upvotes: 0

trappski
trappski

Reputation: 1060

the matches < 5 will Always be reached Before the matches == 1 meaning it will never trigger the second if. so either you change order of the if's or add a Another argument to your less than 5 i.e (matches < 5 && matches > 1).

Upvotes: 0

Related Questions