This question already has an answer here:
- How do I compare strings in Java? 23 answers
Hello stackoverflow community!
I'm having an issue trying to figure out why my do while loop continues to loop even when I am entering the correct condition to break it?
For an example when entering "rock" it still asks me to enter in a valid choice...
Here is my code:
boolean isRight = false;
System.out.println( "Enter Your Name");
Scanner sc = new Scanner(System.in);
String playerName = sc.nextLine();
String UserChoice;
do{
System.out.println("Enter in rock, paper or scissors:");
Scanner user = new Scanner(System.in);
UserChoice = user.nextLine();
if( UserChoice != "rock" || UserChoice != "paper" || UserChoice != "scissors"){
System.out.println("You must enter either rock, paper or scissors. Try again: ");
}else{
isRight = true;
}
}while(isRight==false);
I would really appreciate the help!
Aucun commentaire:
Enregistrer un commentaire