0

I'm a new programmer and trying to teach myself Java by doing random projects. Below is a "Rock, Paper, Scissors" game and the issue that I'm facing is after printing "a", the program ends and does not continue onto the if else statements below. Any help that can be given would be greatly appreciated.

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        System.out.println("Hello & Welcome to Rock, Paper, Scissors. What's your name?");
        Scanner scan = new Scanner(System.in);
        String userChoice = scan.nextLine();
        System.out.println("Hello, " + userChoice + ". Let's start a game!");
        Scanner scan = new Scanner(System.in);
        System.out.println("Choose one: Rock, Paper, Scissors");
        String userFirstChoice = scan.nextLine();
        System.out.println("You chose " + userFirstChoice);

        double a = Math.random();
        System.out.println(a);



        if (a >= 0.00 && a<= 0.3){
            if ( userFirstChoice== "Rock"){
                System.out.println("Rock vs Rock: TIE");
            }
            else if (userFirstChoice == "Paper"){
                System.out.println("Rock vs Paper: YOU LOSE!");
            }
            else if (userFirstChoice == "Scissors"){
                System.out.println("Rock vs Scissors: YOU WIN!");
            }

        }
        else if (a>=0.3 && a<=0.6){
            if(userFirstChoice == "Paper"){
                System.out.println("Paper vs Paper: TIE!");
            }
            else if (userFirstChoice == "Rock"){
                System.out.println("Rock vs Paper: YOU LOSE!");
            }
            else if(userFirstChoice == "Scissors"){
                System.out.println("Scissors vs Paper: YOU WIN!");
            }

        }
        else if (userFirstChoice == "Scissors"){
            System.out.println("Scissors vs Scissors: TIE!");
        }
        else if (userFirstChoice == "Paper"){
            System.out.println("Paper vs Scissors: YOU LOSE!");
        }
        else if (userFirstChoice == "Rock") {

            System.out.println("Rock vs Scissors: YOU WIN!");
        }

    }


}
Micho
  • 3,929
  • 13
  • 37
  • 40
GirlWhoCodes
  • 61
  • 1
  • 1
  • 4

1 Answers1

2

You can't use == to compare strings in Java. (== compares the references, and the operands to == could be references to different strings even though the string contents are identical.)

Use userFirstChoice.equals("Scissors") etc. instead.

Your use of the relational operators on the double types is correct.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483