0

So I'm creating a simple rock paper scissors and I have made a mistake recently. I'm just beginning to learn and I made a mistake and lost track of where. I'd really really appreciate any pointers of where I made a mistake. When it prints out the prompt, I type in what I want, and it just prints it out again. Thank you!

import java.util.Scanner;
import java.util.Random;
import java.io.*;
public class rock_Paper_Scissor {
    public static void main (String[] args) {
        String playerhand;
        boolean x =  true;
        Scanner input = new Scanner(System.in);
        Random num = new Random();
        int rand = num.nextInt(2) + 1;
        System.out.println("I challenge you to Rock Paper Scissor");
        System.out.println("If you want to quit, type exit twice");
        System.out.println("Type Rock, Paper, or scissor");
        playerhand = input.nextLine();
        String hands = playerhand.toLowerCase();
        while (x == true) {
            if (hands == "rock") {              
                if (rand == 1) {
                    System.out.println("Rock vs. Rock: TIE");
                } else if (rand == 2) {
                    System.out.println("Rock vs. Scissor: YOU WIN");
                } else if (rand == 3) {
                    System.out.println("Rock vs. Paper: YOU LOSE");
                }
            } 
            else if (hands == "paper") {
                if (rand == 1) {
                    System.out.println("Paper vs. Rock: YOU WIN");
                } else if (rand == 2) {
                    System.out.println("Paper vs. Scissor: YOU LOSE");
                } else if (rand == 3) {
                    System.out.println("Paper vs. Paper: TIE");
                }
            } 
            else if (hands == "scissor") {
                if (rand == 1) {
                    System.out.println("Scissor vs. Rock: YOU LOSE");
                } else if (rand == 2) {
                    System.out.println("Scissor vs. Scissor: TIE");
                } else if (rand == 3) {
                    System.out.println("Scissor vs. Paper: YOU WIN");
                }
            }
            else if (hands == "exit") {
                System.out.println("Thank you for playing!");
                x = false;
            }
            System.out.println("Please type your hand to play again: ");
            hands = input.nextLine();
        }
    }
}
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
J.Doe
  • 35
  • 5
  • Time to learn how to debug your own code. Hopefully you are using a decent IDE that has step-by-step debug capabilities. – PM 77-1 Feb 12 '16 at 06:26

1 Answers1

1

In your all if condition try to use eqauls() method instead of == like this:

  if ("rock".equals(hands)) { 
   ...
   else if ("paper".equals(hands)) {
   ...     
    else if ("scissor".equals(hands )) {
       ...
    else if ("exit".equals(hands)) {

.equals() is used to compare strings values. But == compare with references strings.

Abdelhak
  • 8,299
  • 4
  • 22
  • 36