Linked Questions

Popular Questions

Why does Java skip over parts of this If statement

Asked by At

I have made a dropdown menu where you can choose if you want to play against a friend or against the computer, and this is were I run into problems.

The program skips over a part of the if statement, and goes to the else statement further below and I do not know why? They are basically the same.

I wanted to check if it would run if I clicked the PVC option, so I made it print out hello when it comes to that, and it does, but it just skips over the rest of the statement after the for loop. It is supposed to set the buttons to X and ÅO, but goes to the normal version where it gets set to X and O instead.

PVC:

if (menu.get(1).equals(e.getSource())) {

    System.out.println("hello");
    for (int i = 0; i < 9; i++) {
        score.set(i,0);
        knapper.get(i).setText(" ");
        count=1;
        Gamechoice=true;
    }

    for (int i = 0; i < 9; i++) {
        if (knapper.get(i).equals(e.getSource())) {
            count += 1;

            if (count % 2 == 0) {
                if (knapper.get(i).getText() == "X" || knapper.get(i).getText() == "O") {
                    System.out.println("Button allready pressed");
                } else {
                    System.out.println("Pressed " + (i + 1));

                    System.out.println(e.getSource());
                    knapper.get(i).setText("X");
                    score.set(i, 1);
                }
            } else if (count % 2 != 0) {

                if (knapper.get(i).getText() == "X" || knapper.get(i).getText() == "O") {
                    System.out.println("Button allready pressed");
                } else {
                    System.out.println(e.getSource());
                    knapper.get(i).setText("ÅO");
                    score.set(i, -1);
                }
            }
        }
    }
} else{
    for (int i = 0; i < 9; i++) {
        if (knapper.get(i).equals(e.getSource())) {
            count += 1;

            if (count % 2 == 0) {
                if (knapper.get(i).getText() == "X" || knapper.get(i).getText() == "O") {
                    System.out.println("Button allready pressed");
                } else {
                    System.out.println("Pressed " + (i + 1));

                    System.out.println(e.getSource());
                    knapper.get(i).setText("X");
                    score.set(i, 1);
                }
            } else if (count % 2 != 0) {
                if (knapper.get(i).getText() == "X" || knapper.get(i).getText() == "O") {
                    System.out.println("Button allready pressed");
                } else {
                    System.out.println(e.getSource());
                    knapper.get(i).setText("O");
                    score.set(i, -1);
                }
            }
        }
    }
}

Related Questions