Search code examples
javaswingjoptionpane

doClick inside JOptionPane


I am a beginner to java, and I need some help. This is my code.

import java.util.*;

import javax.swing.*;

import java.awt.*;

public class SWG1 {

  public static void main(String[] args) {

      int pizza = 0;
      while(pizza == 0){    
      String[] options = { "Easy", "Medium", "Hard" };

      JOptionPane.showMessageDialog(null, "Scrable", "Game", JOptionPane.PLAIN_MESSAGE);
      String fn = JOptionPane.showInputDialog("Choose your level \n", easyb.doClick() ,mediumb.doClick(), hardb.doClick() , "\n \n Easy are words you know everyday \n Medium are words that you have seen before \n Hard are words that you have rarley seen in your life"); 


      String[] easy = {"the","car","jump","that","have","with","on","to","you","this","from","game","video","ball","about","which","know","people","year","get","also"};
      String[] medium = {"abolish","abuse","absurb","access","accomplish","achievement","aggressive","bland","bungle","challenge","combine","crave","frigid","gorge","hazy","oasis","perish","retire","seldom","tropical","vivid","withdraw"};
      String[] hard = {"allusion","bard","characterization","indirect","direct","colloquial","dialect","elegy","farce","genre","humor","jargon","tone","vernacular","unbashed"};

      int x = 0;

      while(x == 0){
         if(fn.equals("Easy") || fn.equals("easy")){
             int index = new Random().nextInt(easy.length);

             String randomWord = easy[index];
             //System.out.println("Random word: '" + randomWord + "'. It is of length: " + randomWord.length());
             int c = 0;

             while(c == 0){
                Random r = new Random();
                randomWord = scramble( r, randomWord );
                if(randomWord.equals(easy[index])){
                    c = 0;
                }
                else{
                    c = 1;
                }
            }


            int b = 0;
            while(b == 0){
                String gn = JOptionPane.showInputDialog(randomWord);

                if(gn.equals(easy[index])){
                    JOptionPane.showMessageDialog(null, "Correct", "Good Job", JOptionPane.PLAIN_MESSAGE);
                    b = 1;
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Sorry, you are wrong", "Bad Job", JOptionPane.PLAIN_MESSAGE);
                    String hn = JOptionPane.showInputDialog("Would you like to quit \n Yes \n No");
                    if(hn.equals("Yes") || hn.equals("yes")){
                        b = 1;
                    }
                }
        }
        }

        if(fn.equals("Medium") || fn.equals("medium")){
            int index1 = new Random().nextInt(medium.length);

            String randomWord1 = medium[index1];
            ///System.out.println("Random word: '" + randomWord1 + "'. It is of length: " + randomWord1.length());

            int q = 0;

            while(q == 0){
                Random r1 = new Random();
                randomWord1 = scramble( r1, randomWord1 );
                if(randomWord1.equals(easy[index1])){
                    q = 0;
                }
                else{
                    q = 1;
                }
            }

            int z = 0;
            while(z == 0){
                String gn1 = JOptionPane.showInputDialog(randomWord1);

                if(gn1.equals(easy[index1])){
                    JOptionPane.showMessageDialog(null, "Correct", "Good Job", JOptionPane.PLAIN_MESSAGE);
                    z = 1;
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Sorry, you are wrong", "Bad Job", JOptionPane.PLAIN_MESSAGE);
                    String hn1 = JOptionPane.showInputDialog("Would you like to quit \n Yes \n No");
                    if(hn1.equals("Yes") || hn1.equals("yes")){
                        z = 1;
                    }
                }
        }
        }

        if(fn.equals("Hard") || fn.equals("hard")){
            int index2 = new Random().nextInt(hard.length);

            String randomWord2 = hard[index2];
            //System.out.println("Random word: '" + randomWord2 + "'. It is of length: " + randomWord2.length());

            int h = 0;

            while(h == 0){
                Random r2 = new Random();
                randomWord2 = scramble( r2, randomWord2 );
                if(randomWord2.equals(easy[index2])){
                    h = 0;
                }
                else{
                    h = 1;
                }
            }

            int y = 0;

            while(y == 0){
                String gn2 = JOptionPane.showInputDialog(randomWord2);

                if(gn2.equals(easy[index2])){
                    JOptionPane.showMessageDialog(null, "Correct", "Good Job", JOptionPane.PLAIN_MESSAGE);
                    y = 1;
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Sorry, you are wrong", "Bad Job", JOptionPane.PLAIN_MESSAGE);
                    String hn2 = JOptionPane.showInputDialog("Would you like to quit \n Yes \n No");
                    if(hn2.equals("Yes") || hn2.equals("yes")){
                        y = 1;
                    }
                }
            }
        }

        String quitn = JOptionPane.showInputDialog("Would you like to continue \n Yes \n No");

        if(quitn.equals("No") || quitn.equals("no")){
            x = 1;
        }

        }
        String quitn1 = JOptionPane.showInputDialog("Would you like to quit the game \n Yes \n No");

        if(quitn1.equals("Yes") || quitn1.equals("yes")){
            System.exit(1);
        }
    }
    }


    public static String scramble( Random random, String inputString )
    {
       char a[] = inputString.toCharArray();

       for( int i=0 ; i<a.length-1 ; i++ )
       {
           int j = random.nextInt(a.length-1);
           // Swap letters
           char temp = a[i]; a[i] = a[j];  a[j] = temp;
       }       

       return new String( a );
   }
}

I need help with this line

String fn = JOptionPane.showInputDialog("Choose your level \n", easyb.doClick() ,mediumb.doClick(), hardb.doClick() , "\n \n Easy are words you know everyday \n Medium are words that you have seen before \n Hard are words that you have rarley seen in your life");

I want the output to look like exactly this

   Choose your level 

   Easy Medium Hard (You can click on these words)

    Easy are words you know everyday
    Medium are words that you have seen before
    Hard are words that you have rarley seen in your life

I also want these if statments to be changed so that it is equal to whatever I click

if(fn.equals("Easy") || fn.equals("easy"))

if(fn.equals("Medium") || fn.equals("medium"))

if(fn.equals("Hard") || fn.equals("hard"))

Please write you answer in code and not in writing. Thank you.


Solution

  • Are you looking for something like this?

    String[] difficultyOptions = new String[] {"Easy", "Medium", "Hard"};
    int response = JOptionPane.showOptionDialog(null, "Message:  Choose Your Difficulty:", "Title:  Difficulty Selection",
        JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
        null, options, options[0]);
    
    switch(response) {
        case 0:
            //this is for easy
            break;
        case 1:
            //this is for medium
            break;
        case 2:
            //this is for hard
            break;
    }