0

So I have been watching some "thenewboston" on youtube and I just started learning java like an hour ago and I seem to not be able to find what my problem is. (There is no error message)

package rockPaperScissors;

import java.util.Scanner;
import java.util.Random;

public class RockPaperScissors {
    public static void main(String args[]){
    String playerChose;
    String computerChose;
    String winner;
    while(true){
        System.out.println("Welcome to rock paper scissors!");
        System.out.println("Please enter \"rock\", \"paper\", or \"scissors\"");
        Scanner playerChoice = new Scanner (System.in);
        playerChose = playerChoice.nextLine();
        Random computerChoice = new Random();
        int computer = computerChoice.nextInt(3) + 1;
        switch(computer){
            case 1:
                computerChose = "rock";
                System.out.println("Computer chose rock!");
                break;
            case 2:
                computerChose = "paper";
                System.out.println("Computer chose paper!");
                break;
            case 3:
                computerChose = "scissors";
                System.out.println("Computer chose scissors!");
        }

        computerChose = new String();
        winner = new String();

        if(playerChose=="rock" && computerChose=="scissors" || playerChose=="paper" && computerChose=="rock" || playerChose=="scissors" && computerChose=="paper"){
            winner="player";
        }

        if(playerChose==computerChose){
            winner="NoWinner";
        }

        if(computerChose=="rock" && playerChose=="scissors" || computerChose=="paper" && playerChose=="rock" || computerChose=="scissors" && playerChose=="paper"){
            winner="computer";
        }

        if(winner!="NoWinner"){
            System.out.println(winner+" won!");
        }else{
            System.out.println("Game tied!");
        }
    }
}
}

Yes, I am new to this java stuff and I have no idea why this code is not working because I thought for sure it would work. I'm sure it's just something stupid that I need to learn. Since I come from Python this is a lot more complex because there are a lot of rules to java that don't exist in Python.

1 Answers1

2

To compare Strings, instead of == you must use equals() method:

str1.equals(str2)

When you use == you are comparing memory references, since String is not a primitive data type in Java. Try searching about it on Google.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73