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.");
}
}
}