1

I'm working on a JPanel project where you play the computer in Rock, Paper, Scissors. I'm using an enum for the choices. I have it set up like so

enum Choices {rock, paper, scissors}

And now, to set the "players choice" to be whatever button they press, I have

if (event.getSource() == rock){        //Makes it so when you press rock button, your choice is rock
    Choice playerChoice = Choice.rock; 
}

But now when I try to do

if (playerChoice == Choice.rock)
    if (cpuChoice == Choice.scissors)
        playerWon = true;

.. and so on for each of the options, I get an error on "playerChoice" saying it cannot find symbol. Any ideas? Thanks in advance!

Edit: It was suggested "Using variables outside of an if statement", but on that, the person was initializing and instantiating the variable inside. I have already initialized it the enum.

Cythix
  • 23
  • 3

1 Answers1

1

You need to change the scope of your Choice playerChoice.

So do:

Choice playerChoice
if (event.getSource() == rock){ 
   playerChoice = Choice.rock; 
}

if (playerChoice == Choice.rock)
    if (cpuChoice == Choice.scissors)
        playerWon = true;
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156