0

I'm a newbie at programming and I need to add a JOptionPane that says: "Play again?". With "Yes" and "No" as the options.

If "Yes", the game will start again. If "No", the game will terminate.

Any help would be much appreciated. Thanks in advance!

import javax.swing.JOptionPane;
import java.util.Random;

public class JackAndPoy {


    public static void main(String[] args)
    {
        String computerSide,
               userSide;

         JOptionPane.showMessageDialog(null, " Play the Game \"Rock, Paper,  Scissors?\"");

         computerSide = ComputerChoice();
         userSide = UserChoice();


        if (userSide != null)
        {
            JOptionPane.showMessageDialog(null, "The computer's choice is " + computerSide + ".");

            Winner(computerSide, userSide);
        }

         else {
            JOptionPane.showMessageDialog(null, "Error: Improper UserEntry. Please enter either" + 
                                        " 'rock', 'paper', or'scissors'.");
        }
    }

     public static String ComputerChoice()
     {
         byte computerChoice;

         String computerChoiceString = "";

         Random choiceGenerator = new Random();

         computerChoice = (byte)(choiceGenerator.nextInt(3) + 1);

         switch (computerChoice)
         {


             case 1:
             { 
                 computerChoiceString = "rock";
                 break;
             }


             case 2:
             {
                computerChoiceString = "paper";
                break;
             }

             case 3:
             {
                computerChoiceString = "scissors";
                break;
             }

         }

         return computerChoiceString;
    }

    public static String UserChoice()
    {
        String userChoice,
               userChoiceLowerCase;

        userChoice =
                JOptionPane.showInputDialog("Choice: [rock][paper][scissors]");

        if (userChoice.equalsIgnoreCase("rock") || userChoice.equalsIgnoreCase("paper")
                || userChoice.equalsIgnoreCase("scissors"))
        {
            userChoiceLowerCase = userChoice.toLowerCase();
        }

        else {
            userChoiceLowerCase = null;
        }

        return userChoiceLowerCase;
    }

 public static void Winner(String computerSide, String userSide)
  {

        if (computerSide.equals(userSide)) {
            JOptionPane.showMessageDialog(null, "The game has to be played again, because we have a tie.");
        }

            else if (computerSide.equalsIgnoreCase("rock") && userSide.equalsIgnoreCase("paper")) {
            JOptionPane.showMessageDialog(null, "You win. Paper covers rock.");
        }


        else if (computerSide.equalsIgnoreCase("rock") && userSide.equalsIgnoreCase("scissors")) {
            JOptionPane.showMessageDialog(null, "You lose. Rock crushes scissors.");
        }


        else if (computerSide.equalsIgnoreCase("paper") && userSide.equalsIgnoreCase("rock")) {
            JOptionPane.showMessageDialog(null, "You lose. Paper covers rock.");
        }


        else if (computerSide.equalsIgnoreCase("paper") && userSide.equalsIgnoreCase("scissors")) {
            JOptionPane.showMessageDialog(null, "You win. Scissors cuts paper.");
        }


        else if (computerSide.equalsIgnoreCase("scissors") && userSide.equalsIgnoreCase("rock")) {
            JOptionPane.showMessageDialog(null, "You win. Rock crushes scissors.");
        }


        else {
            JOptionPane.showMessageDialog(null, "You lose. Scissors cuts paper.");
        }
    }
}
1

1 Answer 1

1

You need a do-while loop, which performs an action and then checks for condition to do it again. This has a difference with regular while loop. do-while loops have to perform that block of code at least once. So change your main method like this:

public static void main(String[] args)
    {
    JOptionPane.showMessageDialog(null,
            " Play the Game \"Rock, Paper,  Scissors?\"");

    String computerSide, userSide;

    do
        {
        computerSide = ComputerChoice();
        userSide = UserChoice();
        if(userSide != null)
            {
            JOptionPane.showMessageDialog(null, "The computer's choice is "
                    + computerSide + ".");
            Winner(computerSide, userSide);
            }
        else
            {
            JOptionPane.showMessageDialog(null,
                    "Error: Improper UserEntry. Please enter either"
                            + " 'rock', 'paper', or'scissors'.");
            }
        }
    while(JOptionPane.showConfirmDialog(null, "Play Again?", "Rock-Paper-Scissors", JOptionPane.YES_NO_OPTION) == 0);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.