0

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.

M.J
  • 21
  • 3
  • 1
    Use the debugger to step through the code to see where it's not doing what you expect. – Ken White Apr 07 '18 at 23:54
  • I found out that a bracket for the first if-statement was misplaced. I fixed it, and there are no issues the debugger can find. Now it keeps repeating the question forever. – M.J Apr 08 '18 at 00:03
  • The debugger allows you to step through the code line by line as it runs. That will tell you exactly why it keeps repeating the question forever. Try again. – Ken White Apr 08 '18 at 00:16

2 Answers2

1

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);
}
Raymo111
  • 514
  • 8
  • 24
  • It should. You will not be able to make comparisons in java of strings with the == function. – Raymo111 Apr 08 '18 at 00:24
  • No problem. Feel free to reply to this thread if you have any additional questions. Thanks for accepting my answer. Also, make sure you understand every part of the code that you use; only then will you learn. – Raymo111 Apr 08 '18 at 01:17
  • I added the breaks but the program now asks at least twice, even if the first input was r,s, or p. – M.J Apr 08 '18 at 01:20
  • This is my first question, and my reputation is too low to ask another until tomorrow. Is the issue possible to explain verbally? – M.J Apr 08 '18 at 01:34
0

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;
    }
Zachary
  • 1,693
  • 1
  • 9
  • 13