0

I wanna say excuse me first for my lack english. I am not a native speaker.

So, I am creating a rock scissor paper game with GUI in Java.

The algorithm is very simple. I will get a showOptionDialog with three buttons. (rock, scissor, paper) When I click one of the three buttons, Java will compare my choice and computer's choice. Computer will choose random number between 1~3

I am almost finished with my GUI but I have some problem with showOptionDialog

private String[]a = {"Rock","Paper","Scissor"};
private int userChoice;
/* some codes 
 */
   public void actionPerformed(ActionEvent e){
    if(e.getSource()==play){
        userChoice = JOptionPane.showOptionDialog(null,"Rock Paper Scissor","Choose",JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE,null,a,a[1]);
        if(userChoice==a[0] )
    }
}

I tried something like this but as you know java shows me an incomporable types int and String. Any good idea how should I do?

ProgramLover
  • 91
  • 2
  • 9
  • What is the exact error message which you get? – Code-Apprentice Apr 22 '17 at 22:55
  • @Code-Apprentice asI mentioned, before compiling, Java shows an incomporable types error. After compiling I don't know now. I am not finished with my code and I cannot compile now. – ProgramLover Apr 22 '17 at 22:59
  • @Code-Apprentice The problem is I know why java shows an incomporable types but I have no idea how should I do in other way – ProgramLover Apr 22 '17 at 23:00
  • Please edit your question to show the **exact** error message **word for word**. By summarizing, you are leaving out important information that will be helpful for future readers to understand what the problem is. – Code-Apprentice Apr 22 '17 at 23:00
  • @Code-Apprentice ok ok thanks I will do that – ProgramLover Apr 22 '17 at 23:07

1 Answers1

1

As you know, JOptionPane.showOptionDialog() returns an int, not a String. So you cannot compare userChoice to one of the String elements of a. Instead, use userChoice as the index to get the choice:

if (userChoice != JOptionPane.CLOSED_OPTION) {
    String choice = a[userChoice];
    // Do something with choice
}

To determine the results of the game, you can just as easily use userChoice directly, rather than the String representation. For example, instead of "rock beats scissors" think of it as "0 beats 1".

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268