Hi, Im really new to java and i'm trying to write a simple rock paper scissors game, and i'm having a problem. I have to incorporate a JOptionPane three button box where the user selects either rock, paper or scissors. The problem is that i don't think im retrieving the information correctly from the buttons in the box as the above code always produces the result of "Invalid user input" regardless of whatever of the three choices that i pick. What am i doing wrong? how do i call out the information from the button the user picks?

In addition The program also has to report what the computer chose, the users choice, who won that round, and game tally. The game tally shows number of rounds, wins/losses for each player and number of ties. when one player has won an advantage of two rounds, report the game winner(player or computer)and that the game loop is ending.

also after the game is completed, i ask the user if he/she wishes to play again. I also have to incorporate a JOptionPain box with yes/no buttons. if the user selects yes, the program loops back to start a new game and if the user selects no, a goodbye message is given stating the number of games played.

import javax.swing.JOptionPane;
import java.util.Random;
import java.util.Scanner;
public class P1 {
    public static void main(String[] args) {
            int computerChoice;
            String computerPick;


        JOptionPane.showMessageDialog(null, "Welcome to my Rock, Paper, Scissors game!\n press ok to play!");

        //Asking User to choose Rock, paper or Scissors
            Object[] options = {"Rock",
                    "Paper",
                    "Scissors"};
                    int n = JOptionPane.showOptionDialog(null,
                            "Rock, Paper or "
                            + "Scissors?",
                            "Choose One!",
                    JOptionPane.YES_NO_CANCEL_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    options,
                    options[2]);

                Random generator = new Random();
                computerChoice=generator.nextInt(3);

                    switch (computerChoice)
                        {
                                case 0:
                            {
                                computerPick = "Rock";
                                break;
                            }
                                case 1:
                            {
                                computerPick = "Paper";
                                break;
                            }
                                case 2:
                            {
                                computerPick = "Scissors";
                                break;
                            }
                                default:
                            {
                                computerPick = "will not happen";
                            } 
                        }

                        if (options.equals(computerPick))
                    {
                                System.out.println("It's a tie!");
                    }
                        else if (options.equals("Rock"))
                    {
                                if (computerPick.equals("Scissors"))
                                System.out.println("Rock crushes scissors. You win!!");
                                     else if (computerPick.equals("Paper"))
                                     System.out.println ("Paper eats rock. You lose!!");
                    }
                                     else if (options.equals("Paper"))
                        {
                        if (computerPick.equals("Scissors"))
                            System.out.println ("Scissor cuts paper. You lose!!");
                                        else if (options.equals("Rock"))
                                    System.out.println ("Paper eats rock. You win!!");
                        }
                                    else if (options.equals("Scissors"))
                                            {
                                                if (computerPick.equals("Paper"))
                                                System.out.println ("Scissor cuts paper. You win!!");
                                    else if (computerPick.equals("Rock"))
                                            System.out.println ("Rock breaks scissors. You lose!!");
                                            }
                                                else
                                                    {
                                                        System.out.println ("Invalid user input.");
                                                    }



        }
    }

Edited 4 Years Ago by peter_budo: Please do not use CODE SNIPPET for posting questions. Keep it as DISCUSSION THREAD instead

Try debugging your code by adding some printlns to show the values of variables and the execution flow. 

You need to format the code better so that }s are beneath its paired }

Your formatting  makes it very hard to read and understand the logic of the code.

Try debugging your code by adding some printlns to show the values of variables and the execution flow.

Sorry about this post. The forum's formatting doesn't allow me to post without making it code????

Edited 4 Years Ago by NormR1

import javax.swing.JOptionPane;
import java.util.Random;
import java.util.Scanner;
public class P1 {
    public static void main(String[] args) {
            int computerChoice;
            String computerPick;


        JOptionPane.showMessageDialog(null, "Welcome to my Rock, Paper, Scissors game!\n John Acosta Java CIS 2235\n Chapter 4 pg 153\n press ok to play!");

        //Asking User to choose Rock, paper or Scissors
            Object[] possibilities = {"Rock", "Paper", "Scissors"};
String s = (String)JOptionPane.showInputDialog(
                    null,
                    "Choose one!:\n",
                    "Customized Dialog",
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    possibilities,
                    "Rock");

                Random generator = new Random();
                computerChoice=generator.nextInt(3);

                    switch (computerChoice)
                        {
                                case 0:
                            {
                                computerPick = "Rock";
                                break;
                            }
                                case 1:
                            {
                                computerPick = "Paper";
                                break;
                            }
                                case 2:
                            {
                                computerPick = "Scissors";
                                break;
                            }
                                default:
                            {
                                computerPick = "will not happen";
                            } 
                        }

                if (s.equals(computerPick))
                    {
                     System.out.println("It's a tie!");
                    }
                    else if (s.equals("Rock"))
                    {
                     if (computerPick.equals("Scissors"))
                    System.out.println("Rock demolishes the crap out of scissors. You win!!");
                         else if (computerPick.equals("Paper"))
                         System.out.println ("In real life paper would never beat rock but in this game it does. You lose!!");
                    }
                    else if (s.equals("Paper"))
                    {
                    if (computerPick.equals("Scissors"))
                        System.out.println ("Scissor dices paper. You lose!!");
                        else if (s.equals("Rock"))
                        System.out.println ("you lucky bastard, paper would never beat rock in reality but whatever. You win!!");
                    }
                    else if (s.equals("Scissors"))
                    {
                     if (computerPick.equals("Paper"))
                     System.out.println ("Scissor dices paper. You win!!");
                     else if (computerPick.equals("Rock"))
                     System.out.println ("Rock demolishes the crap out of scissors. You lose!!");
                    }
                    else
                    {
                     System.out.println ("Invalid user input.");
                    }



        }
    }

I'm sorry I'm very new to this and im just getting acquainted with the language. I changed the three button dialog box to a dropdown menu and that seemed to work!

Edited 4 Years Ago by Jdan2388

You also changed what the code was comparing: options vs s

Look at the difference between the definitions of those two variables.

here is the modified program

very well made :)

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

public class Pa {
    public static void main(String[] args) {

        JOptionPane
                .showMessageDialog(
                        null,
                        "Welcome to my Rock, Paper, Scissors game!\n John Acosta Java CIS 2235\n Chapter 4 pg 153\n press ok to play!");

        int flag = 1;
        do {
            play();

            Object[] options = { "Yes, please", "No, thanks" };
            flag = JOptionPane.showOptionDialog(null,
                    "would you like to play again?", "Play again?",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
                    null, options, options[1]);

        } while (flag == 0);

        JOptionPane.showMessageDialog(null, "Thank you for playing");

    }

    static void play() {
        int computerChoice;
        String computerPick;

        // Asking User to choose Rock, paper or Scissors
        Object[] possibilities = { "Rock", "Paper", "Scissors" };
        String s = (String) JOptionPane.showInputDialog(null, "Choose one!:\n",
                "Customized Dialog", JOptionPane.PLAIN_MESSAGE, null,
                possibilities, "Rock");

        Random generator = new Random();
        computerChoice = generator.nextInt(3);

        switch (computerChoice) {
        case 0: {
            computerPick = "Rock";
            break;
        }
        case 1: {
            computerPick = "Paper";
            break;
        }
        case 2: {
            computerPick = "Scissors";
            break;
        }
        default: {
            computerPick = "will not happen";
        }
        }

        if (s.equals(computerPick)) {
            System.out.println("It's a tie!");
        } else if (s.equals("Rock")) {
            if (computerPick.equals("Scissors"))
                System.out
                        .println("Rock demolishes the crap out of scissors. You win!!");
            else if (computerPick.equals("Paper"))
                System.out
                        .println("In real life paper would never beat rock but in this game it does. You lose!!");
        } else if (s.equals("Paper")) {
            if (computerPick.equals("Scissors"))
                System.out.println("Scissor dices paper. You lose!!");
            else if (s.equals("Rock"))
                System.out
                        .println("you lucky #######, paper would never beat rock in reality but whatever. You win!!");
        } else if (s.equals("Scissors")) {
            if (computerPick.equals("Paper"))
                System.out.println("Scissor dices paper. You win!!");
            else if (computerPick.equals("Rock"))
                System.out
                        .println("Rock demolishes the crap out of scissors. You lose!!");
        } else {
            System.out.println("Invalid user input.");
        }
    }
}
This article has been dead for over six months. Start a new discussion instead.