This question already has an answer here:
- How do I compare strings in Java? 23 answers
I am novice at Java programming and am writing a short code to run a game of "Nim," a game where counters are removed from one of three stacks by individual players. Because I am dealing with manual inputs, I have created two methods to handle this:
-
checkNumInput()
-
checkPileInput()
I am pretty sure that the problem comes from the if statement within the checkPileInput() function, but I am not sure.
Any help would be much appreciated (even if I need to look to another part of my code)! :)
checkPileInput() method below:
System.out.println(playerHold + ", which pile to take from (A, B or C)?");
whichPile = keyboard.next();
if((whichPile == "A")^(whichPile == "B")^(whichPile == "C")^(whichPile == "a")^(whichPile == "b")^(whichPile == "c")){
break;
}
else{
System.out.println("\nPlease choose A, B or C."); //Executed even when checkLoop is equal to one of the variables in the if statement above.
}
Potentially troublesome portion of main method below:
playerHold = p1;
NimRedo.checkPileInput();
NimRedo.checkNumInput();
if(total == 1){
System.out.println("Player 1 has won!");
break;
}
playerHold = p2;
NimRedo.checkPileInput();
NimRedo.checkNumInput();
P.S. I have yet to start learning of Enum and Exceptions in Java so ideally I would like to stay away from using those at the moment however I am still open to suggestions on them.
Aucun commentaire:
Enregistrer un commentaire