lundi 11 février 2019

Why does Java skip over parts of this If statement [duplicate]

This question already has an answer here:

Excuse my messy code, Im new to this. I am making a tic tac toe in java for uni, and i have made the it so that u can play against a friend or w/e on the same computer. I have made a dropdown menu where u can choose if you want to against a the program, and this is were i run into problems. The program skips over part of the if statement, and goes to the else statement further below. How come? They are basically the same code. 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. Its 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() == "EGGS" || 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);

                    }
                }
            }
        }
    }

Aucun commentaire:

Enregistrer un commentaire