public class User implements GameObject{
private Scanner userScanner;
private int result = 0;
public User() {
userScanner = new Scanner(System.in);
}
public int getGameObject() {
System.out.println("Rock: 1, Paper: 2 or Scissors: 3");
String userInput = userScanner.nextLine();
if (userInput == "1" || userInput == "2" || userInput == "3") {
// User has entered a valid input
switch (userInput) {
case "1":
return 1;
case "2":
return 2;
case "3":
return 3;
}
}
return getGameObject();
}
}
My problem is the result from getGameObject() method. When I am using this in the application its always return 0 or a value I set in the last return (int fex 2). I want to get user values (0,1,2). My plan is to return at the end method itself to get back to the first propmpt line in case wrong selection from the user. In such case method always return method recall and I fall in kind of infinite loop. Could you help?