0

So I am making a memory type game and the problem is the program is not entering the if statement even though I know the values it is checking are equal because I printed both values out before the if statement. They both are 1. I know the if statement is satisfied/true because of this. I know it is not entering the loop because I put a print statement in the loop and do not see the console display that if statement. So I am going to post a small section of code-the code that I think is relevant since people get mad if I post the entire code. If you need the entire code, let me know and I will post it. Anyways, what I get printed to my console is:

1

1

Notice how the statement: "Round finished." is not there when it should be.

Here is the code:

else {
    System.out.println(++numberOn);
    System.out.println(currentPattern.size());
    if (++numberOn==currentPattern.size()) {
        System.out.println("Round finished.");
        score++;
        numberOn=0;
        displayPattern();
    }
    else {
        numberOn++;
    }
}

The program is entering this big/first else statement 100% or else the first 2 statements wouldn't have ran. So I need to know why the if statement, line 4, isn't running when both sides are equal to each other. Thanks in advance.

7
  • By the way, my debugger isn't working for some reason as it must not like gui's. So I have tried using the debugger. Commented May 14, 2018 at 21:20
  • ++numberOn will increment before using the variable. Commented May 14, 2018 at 21:20
  • I know, I want the program to first increase numberOn, before checking if the if statement is true or not. Commented May 14, 2018 at 21:21
  • 1
    You're printing ++numberOn, which increments it. Then you compare it, which increments again. You're adding two to the value. Commented May 14, 2018 at 21:21
  • I imagine your print statement is incrementing it first. Commented May 14, 2018 at 21:22

1 Answer 1

5

++numberOn doesn't simply return the value of numberOn plus one. It also increments the actual value of numberOn. When you do System.out.println(++numberOn), you're adding one to the value of numberOn and printing it. When you later do ++numberOn, you're adding another one to the variable then comparing that value. So do numberOn + 1 instead of ++numberOn, unless you actually mean to mutate the variable.

As a side note, this is why a lot of people (myself included) tend to advocate only doing ++ operations on their own line. So

++numberOn;
if (numberOn == ...) { ... }

I imagine you copy-pasted the ++numberOn into the print statement, not thinking about the implications of incrementing twice. It's an easy mistake to make, so doing such operations on their own line will minimize the risk and make the mutation more apparent.

Sign up to request clarification or add additional context in comments.

3 Comments

Does it increase the value in the if statement? I will delete the print statement then and see what happens.
Yes it does. If you delete the print statement and the problem still occurs, then there's something else going on here.
I see, I got rid of the if statements and it semi works. I get an index out of bounds exception now but I can fix that as I have a pretty good idea why that is happening. Thanks and please upvote this. People already do not understand that it was simply a misunderstanding of code. You're not going to look something up on google if you think a piece of code works a certain way and it must be something else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.