**hello in my java program I had made small "rock" "paper" "scissor" game but it skips all Statement and directly print else statement **
String [] a = {"r","p","s"};
while(true){
System.out.println("enter you move");
String userinput =sc.nextLine();
String comp = a[ran.nextInt(a.length)];
if (userinput == comp) {
System.out.println("oops its a tie");
}
else if (userinput == "r" && comp == "s" || userinput == "p" && comp == "r"
|| userinput == "s" && comp == "p") {
System.out.println("You Win!");
} else {
System.out.println("Computer Win! :" comp);
}
System.out.println("playagin(y/n)");
String playagain = sc.nextLine();
if (!playagain.equals("y")) {
break;
}
}
CodePudding user response:
Use .equals() string method to compare string value , not == operator
CodePudding user response:
To compare a text you need to use equals() for example:
userinput.equals(comp)
Otherwise you just compare a links on objects.
CodePudding user response:
You are confusing the equality operator with the String equals method. This should clarify the issue.