I'm in the process of coding a simple command-line tic-tac-toe game for extra credit in my AP Computer Science I class. I've come across a bit of a hitch...no matter what I enter for playerSymbol when running the program (certain values should exceute one of the "if/else if" blocks), the block of code for my "else" is executed. It compiles fine, however. Could you take a look and see what could be wrong?
Code:
//the charSelection method, which lets the player decide to be an X or an O
public static void charSelection() throws IOException{
/*declares the standard input stream and declares variables
*that determine whether the player is an X or an O*/
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
String playerSymbol;
char compSymbol;
//asks the player to choose X or O
System.out.print("\tWould you like X's or O's? Please choose one ");
System.out.print("(enter X or O):");
playerSymbol = br.readLine();
//ensures the player entered X or O
if (playerSymbol == "X"){
compSymbol = 'O';
game(); //calls method game()
}
else if (playerSymbol == "x"){
playerSymbol = "X";
compSymbol = 'O';
game();
}
else if (playerSymbol == "O"){
compSymbol = 'X';
game();
}
else if (playerSymbol == "o"){
playerSymbol = "O";
compSymbol = 'X';
game();
}
else{
System.out.print("\tPlease enter either X or O.\n");
charSelection();
}
}