Hey guys, im doing a rock paper scissors program for h/w i got it done but im getting an error and i cant figure out whats wrong.

public static void main(String[] args)
   {
       int pcpick;
       int userpick;
       String userChoice = JOptionPane.showInputDialog("1 Rock, 2 Paper, 3 Scissors");
       int choice = Integer.parseInt(userChoice);
       
       switch (choice)
       {
           case 1:
           userpick = 1;
           break;
           case 2:
           userpick = 2;
           break;
           case 3:
           userpick = 3;
           break;
           default:
           System.out.println("ERROR");
           break;
        }
        Random gen = new Random();
        int pcnum;
        pcnum = gen.nextInt(3) + 1;
        switch (pcnum)
        {
            case 1:
            pcpick = 1;
            break;
            case 2:
            pcpick = 2;
            break;
            case 3:
            pcpick = 3;
            break;
            default:
            System.out.println("Error");
            break;
        }
        if (pcpick == 1)
        {
            if(userpick==1)
            System.out.println("You both picked rock, it's a tie!");
            if(userpick==2)
            System.out.println("PC picked rock you picked paper, you win!");
            if(userpick==3)
            System.out.println("PC picked rock you picked scissors, you lose!");
        }
        if (pcpick == 2)
        {
            if(userpick==1)
            System.out.println("PC picked paper you picked rock, you lose!");
            if(userpick==2)
            System.out.println("You both picked paper, it's a tie!");
            if(userpick==3)
            System.out.println("PC picked paper you picked scissors, you win!");
        }
        if (pcpick == 3)
        {
            if(userpick==1)
            System.out.println("PC picked scissors you picked rock, you win!");
            if(userpick==2)
            System.out.println("PC picked scissors you picked paper, you lose!");
            if(userpick==3)
            System.out.println("You both picked scissors, it's a tie!");
        }

The above is all my code... im using BLUEJ. it highlights

if (pcpick == 1)

and states "variable pcpick might not have been initialized." Let me know what you think!
im thinking i didnt do the "cases" right

im thinking i didnt do the "cases" right

You're right - pcpick is not initialized in all of the possible branches of your switch statement - if you get something other than 1, 2, or 3, it's never given a value, and that's not allowed. Easiest solution is to initialize it on declaration:

int pcpick = 0;

or put that assignment in the default branch of your switch.

commented: Helpful! +2

oh gosh you're right -.- i should've seen that! thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.