M.J
M.J

Reputation: 21

java (eclipse) code for rock paper scissors, choosing an input that is simple, and generating an output of rock paper or scissors

I need to create a program that asks the user for a string value and returns a string value of "rock" "paper" or "scissors" if the input was "r" "p" or "s" If the user typed in something different.

 package loopsGamesProject;

    import java.util.Scanner;
    public class LoopsGamesProject_RockPaperScissors {

public static void main(String[] args) {
    String in="-";
    Scanner input= new Scanner(System.in);
    System.out.print("Enter 'r' for rock, and 'p' for paper,'s' for scissors:");
    in=input.next();
    if(in=="r"||in=="p"||in=="s"){
        if(in=="r"){
            in="Rock";
        }
        if(in=="p"){
            in="Paper";
        }
        if(in=="s"){
            in="Scissors";
        }
    while(in!="r"||in!="p"||in!="s") {
    System.out.print("Enter 'r' for rock, and 'p' for paper,'s' for scissors:");
    in=input.next();
    if(in=="r"||in=="p"||in=="s"){
        if(in=="r"){
            in="Rock";
        }
        if(in=="p"){
            in="Paper";
        }
        if(in=="s"){
            in="Scissors";
        }
    }
    }
    input.close();
    System.out.print(in);



    }

}

}

The issue is, it will ask for a variable, but the terminate itself. I've tried adding an "out" variable. When I tried to do this using a do while loop, it kept asking for an input but never returned anything. I can't find the issue.

Upvotes: 0

Views: 2201

Answers (2)

Raymo111
Raymo111

Reputation: 584

When you compare Strings in java, you need to use the .equals() method instead of the == function. This rule applies for all objects in java, String inclusive. EG:

if (in.equals("r"))
    //Rock!

You also need to replace the != and add a break statement to exit the loop. Something like this will do:

while (!(in.equals("r") || in.equals("p") || in.equals("s"))) {
    System.out.print("Enter 'r' for rock, and 'p' for paper,'s' for scissors:");
    in = input.next();
    if (in.equals("r") || in.equals("p") || in.equals("s")) {
        if (in.equals("r"))
            in = "Rock";
        else if (in.equals("p"))
            in = "Paper";
        else
            in = "Scissors";
                break;
    }
}

EDIT: The above prompts twice. This will fix it:

public static void main(String[] args) {
    String in = "";
    Scanner input = new Scanner(System.in);
    while (!(in.equals("Rock") || in.equals("Paper") || in.equals("Scissors"))) {
        System.out.print("Enter 'r' for rock, and 'p' for paper,'s' for scissors:");
        in = input.next();
        if (in.equals("r") || in.equals("p") || in.equals("s")) {
            if (in.equals("r")) {
                in = "Rock";
            }
            if (in.equals("p")) {
                in = "Paper";
            }
            if (in.equals("s")) {
                in = "Scissors";
            }
            break;
        }
    }
    input.close();
    System.out.print(in);
}

Upvotes: 1

Zachary
Zachary

Reputation: 1703

As has been mentioned, you need to compare String equality using the String.equals(Object anObject) - alternatively you may use others methods (compareTo), but the == operator will not suffice (See here why).

On top of this, when you match the String you overwrite the String with the full word - in = 'r'; -> in = 'Rock';. But the condition you use to loop will only terminate when in is r, p or s specifically.

Further, you have some duplicated code there that could be reduced significantly. This is not a bug, but this can become very difficult to manage very quickly.

All things considered:

    while (true) {
        // Get the next input
        in = input.next();

        // Maps the word to the character
        // If a word was not mapped, try again.
        if (in.equals("r"))
            in = "Rock";
        else if (in.equals("p"))
            in = "Paper";
        else if (in.equals("s"))
            in = "Scissors";
        else
            continue;
        // Terminate the loop as you can infer a match was found.
        break;
    }

Upvotes: 0

Related Questions