0

I'm new to Java, and I created some sort of a mini game, and I wanted players to choose whether or not they want to play again, I tried changing my start boolean variable into static type and adding some lines of code, but the code doesn't seem to work, every time I play the game, it did ask me if I want to replay or not, but the problem is that even though I typed "yes" to the console at the end, the game doesn't seem to restart. Can anyone help me please, I would be really appreciated, thanks!

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

//Rocks, papers, scissors game complied by William To.
public class RockPaperScissor {
static int gamePlays = 0;
static boolean start = true;
public static void main(String[] args) {
    while (start){
        @SuppressWarnings("resource")
        Scanner userInput = new Scanner(System.in);
        Random comOutput = new Random();
        System.out.println("Do you choose rock, paper or scissor?");
        String userChoice = userInput.nextLine();
        int comChoice = comOutput.nextInt(2);
        switch (userChoice){

        case "rock":
            if(comChoice == 0){
                System.out.println("I choose rock too! That's a draw!");
            } else if(comChoice == 1){
                System.out.println("I choose paper! I win!");
            } else {
                System.out.println("I choose scissor! You win!");
            }
            break;

        case "paper":
            if(comChoice == 0){
                System.out.println("I choose rock! You win!");
            } else if(comChoice == 1){
                System.out.println("I choose paper too! That's a draw!");
            } else {
                System.out.println("I choose scissor! I win!");
            }
            break;

        case "scissor":
            if(comChoice == 0){
                System.out.println("I choose rock! I win!");
            } else if(comChoice == 1){
                System.out.println("I choose paper! You win!");
            } else {
                System.out.println("I choose scissor too! That's a draw!");
            }
            break;
        default :
            System.out.println("I don't think that's an option.");
            break;
        }
        gamePlays++;
        System.out.println("You've played " + gamePlays + " games.");

               //BELOW IS THE PART I want to ask, what's wrong? 

        System.out.println("Do you want to play again?"); 
        String playAgain = userInput.next();
        if (playAgain == "yes"){
            System.out.println("Restarting game...");
            start = true;
        } else if (playAgain == "no") {
            System.out.println("Quitting game.");
            start = false;
        }
    }
}   
} 

The print out from Eclipse:

Do you choose rock, paper or scissor?  
rock **//my input**    
I choose paper! I win!  
You've played 1 games.  
Do you want to play again?    
yes **//my input**    
Do you choose rock, paper or scissor?  
paper **//my input**    
I choose paper too! That's a draw!  
You've played 2 games.  
Do you want to play again?  
no **//my input**  
Do you choose rock, paper or scissor? 
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
William To
  • 31
  • 1
  • 3

5 Answers5

5

Comparison between strings needs the String#equals instead of ==. So this:

if (playAgain == "yes"){
    System.out.println("Restarting game...");
    start = true;
} else if (playAgain == "no") {
    System.out.println("Quitting game.");
    start = false;
}

Has to become this:

if (playAgain.equals("yes")){
    System.out.println("Restarting game...");
    start = true;
} else if (playAgain.equals("no")) {
    System.out.println("Quitting game.");
    start = false;
}

In order to work properly.

However, I suggest to use String#equalsIgnoreCase, because with equals, the comparison is case-sensitive, so if user inputs i.e. Yes or yEs, playAgain.equals("yes") will return false

UPDATE

As Maroun Maroun says, it is better to use

"yes".equals(playAgain)

and

"no".equals(playAgain)

Because if playAgain is null it won't throw a NullPointerException

BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • 2
    +1. Although I prefer to `"yes".equals(playAgain)` just in case `playAgain` is `null`, it won't be NPE. – Maroun Jun 22 '13 at 09:54
  • 2
    @MarounMaroun I always wondered why people always put the string literal first. Now I know, thanks! – Imre Kerr Jun 22 '13 at 10:01
  • ohhh, so that was the problem... Thanks everyone for helping me out :D , I'm kinda like a newbie so... :) – William To Jun 23 '13 at 06:17
4

Try:

    if ("yes".equals(playAgain)) {
        System.out.println("Restarting game...");
        start = true;
    } else if ("no".equals(playAgain)) {
        System.out.println("Quitting game.");
        start = false;
    }

String#equals() checks the character equality of string, == checks memory reference.

Maroun
  • 94,125
  • 30
  • 188
  • 241
zeratul021
  • 2,644
  • 3
  • 32
  • 45
3

The problem is probably this line

if (playAgain == "yes")

(and the one later for no).

Strings in Java are objects. This means that two strings might have the same content, i.e. contain the same text, but still not be "the same" as far as == is concerned. To compare two strings and see if they contain the same text, use this:

if (playAgain.equals("yes"))
Imre Kerr
  • 2,388
  • 14
  • 34
1

Problem is with the way in which String comparision is done.

playAgain == "yes", This check will check for object equality.

However in this case, value of object needs to be compared, following code will work for you.

playAgain.equals("yes")
Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
Dark Knight
  • 8,218
  • 4
  • 39
  • 58
0

You need compare string using String.equals() method and not ==

The equals method compares for the value of the string and == checks whether both the objects refer to the same same object or not.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • _Also, you need to put break statement after each case in switch block else it won't break out of the switch block and will execute all the following switch cases_ In the code the OP posted, he **is** doing that. – BackSlash Jun 22 '13 at 09:51
  • Instead, you can just upvote the answer and accept it so that it will also help others readers to know it is helpful :) – Prasad Kharkar Jun 23 '13 at 06:37
  • I can't, it requires 15 population. – William To Jun 24 '13 at 16:39