-4

I created this program to run infinite times asking the user to enter a choice(rock or paper or scissor), which seems to work fine. The problem is no else ....if or if ....else statements are satisfied. Whatever input i give it just prints out the else statement in the else statement, (Enter a valid choice). I cant find the mistake i made so ill link my code below..... Thanks in advance.

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

public class apples {
    public static void main(String args[]){

        Scanner input = new Scanner(System.in);
        Random number = new Random();

        String rps[] = {"rock", "paper", "scissor"};
        String player;
        String ai;
        int rand, pscore=0, aiscore=0;

        while(1 < 2){
        System.out.println("Take your pick \nrock \npaper \nscissor");
        player = input.nextLine();

        rand = number.nextInt(3);
        ai = rps[rand];

        System.out.println(ai);

        if(player == ai){
            pscore+=0;
            aiscore+=0;
            System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore);
            System.out.print("\n");
            continue;
        }else{
            if(player == "rock" && ai == "paper"){
                aiscore+=1;
                pscore+=0;
                System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore);
                System.out.print("\n");
                continue;
            }else if(player == "rock" && ai == "scissor"){
                pscore+=1;
                aiscore+=0;
                System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore);
                System.out.print("\n");
                continue;
            }else if(player == "paper" && ai == "rock"){
                pscore+=1;
                aiscore+=0;
                System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore);
                System.out.print("\n");
                continue;
            }else if(player == "paper" && ai == "scissor"){
                aiscore+=1;
                pscore+=0;
                System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore);
                System.out.print("\n");
                continue;
            }else if(player == "scissor" && ai == "rock"){
                aiscore+=1;
                pscore+=0;
                System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore);
                System.out.print("\n");
                continue;
            }else if(player == "scissor" && ai == "paper"){
                aiscore+=0;
                pscore+=1;
                System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore);
                System.out.print("\n");
                continue;
            }else{
                System.out.println("Enter a valid choice");
                System.out.print("\n");
                continue;
            }
        }
        }


    }
}
Baderous
  • 1,069
  • 1
  • 11
  • 32
ganesh vicky
  • 17
  • 1
  • 7
  • 1
    It's better that you post your code here. It will always be here if you do, whereas the contents of the link might not! – LiXie Nov 11 '16 at 14:28
  • Consider looking at this first: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Tim Nov 11 '16 at 14:29
  • Thank you guys. BTW i wasn't sure what to search for the problem i faced. Anyways i updated the code in the pastebin link and its working just fine. – ganesh vicky Nov 13 '16 at 07:20

2 Answers2

0

In java, == tests for reference equality: if the two objects are actually the same object. Two strings holding the same characters will fail this check, which is what's happening in your logic.

See How do I compare strings in Java?

Community
  • 1
  • 1
nvioli
  • 4,137
  • 3
  • 22
  • 38
0

Instead of :

if(player == "rock" && ai == "paper"){
    ...
}

Use

if(player.equals("rock") && ai.equals("paper")){
   ...
}

This will apply to all of your pieces of code which compare Strings.

LiXie
  • 136
  • 2
  • 14