0

This code never prints whether I win, lose, or it's a tie and I can't figure it out. I know hischoice and whichpick are both getting set correctly, but the ifs dont seem to work. Am I using Strings incorrectly or am I comparing them wrong? I am new to Java, so i'm not really sure what Im doing wrong.

import java.util.Scanner;
import java.util.Random;
public class rps {

public static void main(String[] args) {
    int oppchoice;
    String hischoice = "";
    String whichpick;
    String rock = "rock";
    String paper = "paper";
    String scissors = "scissors";
    Scanner mS = new Scanner(System.in);
    Random mR = new Random();

    System.out.print("Type your choice (rock/paper/scissors): ");
    whichpick = mS.nextLine();

    oppchoice = mR.nextInt(3) + 1;
    if (oppchoice == 1) {
        hischoice = rock;
    }
    if (oppchoice == 2) {
        hischoice = paper;
    }
    if (oppchoice == 3) {
        hischoice = scissors;
    }
    System.out.println(hischoice);

    if ((hischoice == rock) && (whichpick == paper)) {
        System.out.println("You win! :)");
    }
    if ((hischoice == paper) && (whichpick == scissors)) {
        System.out.println("You win! :)");
    }
    if ((hischoice == scissors) && (whichpick == rock)) {
        System.out.println("You win! :)");
    }
    if ((hischoice == rock) && (whichpick == scissors)) {
        System.out.println("You lose! :(");
    }
    if ((hischoice == paper) && (whichpick == rock)) {
        System.out.println("You lose! :(");
    }
    if ((hischoice == scissors) && (whichpick == paper)) {
        System.out.println("You lose! :(");
    }
    if ((hischoice == rock) && (whichpick == rock)) {
        System.out.println("It's a tie! :|");
    }
    if ((hischoice == scissors) && (whichpick == scissors)) {
        System.out.println("It's a tie! :|");
    }
    if ((hischoice == paper) && (whichpick == paper)) {
        System.out.println("It's a tie! :|");
    }
}   

}

3 Answers3

2

The == operator checks to see if two objects are exactly the same object. Two strings are different objects, but have the same value (have exactly the same characters in them). So use either .equals() method to compare strings for equality or use the .compareTo() method to test for unequal comparisons. In this case it is easier to work with .equals().

So here is how it should be done

if ((hischoice.equals(rock)) && (whichpick.equals(rock))) {
        System.out.println("It's a tie! :|");
    }
abRao
  • 2,787
  • 1
  • 25
  • 37
1

The rest of the answers are correct, you need to use the equals method to compare two String objects or any other objects.

May i also suggest you to compact these if statements into 1 From what it should be as following:

if ((hischoice.equals(rock)) && (whichpick.equals(rock))) {
        System.out.println("It's a tie! :|");
    }
    if ((hischoice.equals(scissors)) && (whichpick.equals(scissors))) {
        System.out.println("It's a tie! :|");
    }
    if((hischoice.equals(paper)) && (whichpick.equals(paper))){
        System.out.println("It's a tie! :|");
    }

to the following:

if (hischoice.equals(whichpick )) {
    System.out.println("It's a tie! :|");
}
nick
  • 86
  • 4
0

You should use .equals() for string comparison

Leon
  • 988
  • 7
  • 20