Ken Young
Ken Young

Reputation: 33

Java Do While isn't working properly

Sorry I am new to this site so not sure how this will show up. I am trying to make a simple Rock, Paper, Scissors game. After the while statement, if R, P, S isn't entered, the program just does nothing. I want it to loop back to the question at the beginning so a right choice can be entered. Also, how would I enter a print statement like "Invalid Choice Please Retry"?

package rps.gameapp;

import java.util.Scanner;

public class RPSGameApp
{

    public static void main(String[] args)
    {

    Scanner sc = new Scanner(System.in);
    String userChoice;
    String playAgain;
    int randNum = (int) (Math.random() * 3);

    do
        {
        System.out.println("Welcome to Rock, Paper, Scissors Game.");
        System.out.println("Pick R, P, or S.");
        userChoice = sc.nextLine();
        while (!userChoice.equalsIgnoreCase("P")
                && !userChoice.equalsIgnoreCase("R")
                && !userChoice.equalsIgnoreCase("S"));


            String compChoice = "";
        switch (randNum)
            {
            case 0:
                compChoice = "R";
                break;
            case 1:
                compChoice = "P";
                break;
            case 2:
                compChoice = "S";
                break;
            }

        System.out.println("The computer entered \"" + compChoice + "\".");

        if (compChoice.equalsIgnoreCase(userChoice))
            {
            System.out.println("Draw");
            } else if (userChoice.equalsIgnoreCase(userChoice)
                && compChoice.equalsIgnoreCase("S")
                || userChoice.equalsIgnoreCase("P")
                && compChoice.equalsIgnoreCase("R")
                || userChoice.equalsIgnoreCase("S")
                && compChoice.equalsIgnoreCase("P"))
            {
            System.out.println("User Wins");
            } else
            {
            System.out.println("User Loses");
            }

        System.out.print(
                "Do you want to play again? (Y/N)");
        playAgain = sc.nextLine();

        } while (playAgain.equalsIgnoreCase("Y"));

    System.out.println("Thanks for Playing!");

    }
}

Upvotes: 0

Views: 140

Answers (2)

Łukasz Chorąży
Łukasz Chorąży

Reputation: 644

Like Eran said, you need to wrap your do-while loop in another loop, that will keep asking user for correct input. This is fully working code. One thing that could be better is the message after user inputs wrong letter.

Edit: also make sure you draw random number for every iteration.

Edit 2: to change the message depending on user input you can introduce a new variable that will keep the track of number of times you asked user for correct input. If it is 0- it means user is asked the first time and we should print "Welcome" message. It is anything other than 0- you need to ask the user for correct input. After every round we assign zero to the variable again and the cycle repeats. I have implemented this change in the code. Note that this variable can also be a boolean.

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String userChoice;
    String playAgain;
    int iterationNumber;

    while (true) {
        iterationNumber = 0;
        do {
            if (iterationNumber == 0) {
                System.out.println("Welcome to Rock, Paper, Scissors Game.");
                System.out.println("Pick R, P, or S.");

            } else {
                System.out.println("Please enter valid letter.");
                System.out.println("Pick R, P, or S.");
            }
            iterationNumber++;
            userChoice = sc.nextLine();
        } while (!userChoice.equalsIgnoreCase("P")
                && !userChoice.equalsIgnoreCase("R")
                && !userChoice.equalsIgnoreCase("S"));
        String compChoice = "";
        int randNum = (int) (Math.random() * 3);
        switch (randNum) {
            case 0:
                compChoice = "R";
                break;
            case 1:
                compChoice = "P";
                break;
            case 2:
                compChoice = "S";
                break;
        }

        System.out.println("The computer entered \"" + compChoice + "\".");

        if (compChoice.equalsIgnoreCase(userChoice)) {
            System.out.println("Draw");
        } else if (userChoice.equalsIgnoreCase("R")
                && compChoice.equalsIgnoreCase("S")
                || userChoice.equalsIgnoreCase("P")
                && compChoice.equalsIgnoreCase("R")
                || userChoice.equalsIgnoreCase("S")
                && compChoice.equalsIgnoreCase("P")) {
            System.out.println("User Wins");
        } else {
            System.out.println("User Loses");
        }

        System.out.print(
                "Do you want to play again? (Y/N)");
        playAgain = sc.nextLine();
        if (playAgain.equalsIgnoreCase("N")) {
            break;
        }
        iterationNumber = 0;
    }
    System.out.println("Thanks for Playing!");
}

Upvotes: 1

Eran
Eran

Reputation: 393771

It looks like you forgot one do for your inner do while loop.

It should be :

do {
    do {
        System.out.println("Welcome to Rock, Paper, Scissors Game.");
        System.out.println("Pick R, P, or S.");
        userChoice = sc.nextLine();
    } while (!userChoice.equalsIgnoreCase("P") && !userChoice.equalsIgnoreCase("R") && !userChoice.equalsIgnoreCase("S")); 
    ...
} while (playAgain.equalsIgnoreCase("Y"));

Without that inner do (and the curly braces surrounding that loop's body), the inner loop becomes a while loop with an empty body.

Upvotes: 3

Related Questions