Results 1 to 3 of 3
  1. #1
    intEquals_EJ is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Multiple issues with my 1-6 guessing game.

    Hi all,

    First I want to thank all of you for your help. Secondly, I stink at object-oriented programming. I think it has something to do with how I logically think. Lastly, I read through as much forums and did as much google(ing) as I possibly could.

    The program is a guessing game that is from 1 -6. I have the opening message working. The userChoice method works also, but I am trying to add a counter because I know the actual value of "1" is 0.

    Java Code:
     public static void userChoice() //This method allows the user to click a number in the following showOptionDialog
            {
             int getChoice;// this stores the users choice into an integer variable
                
         
                 int mouseClick = JOptionPane.QUESTION_MESSAGE; // this stores the click into a variable
    
            String[] options = { "1", "2", "3", "4", "5", "6", "Exit"}; // END STRING OPTIONS
    
            getChoice = JOptionPane.showOptionDialog (null, "Choose a Figure", "Title", 0, mouseClick, null, options, options[2]);
    
    		    if (getChoice >= 0)
    			 
                  System.out.println ("You clicked " + options[getChoice]);
               // END IF
              else
    			 {
                  System.out.println ("You closed the box.");
              } // END ELSE
       
                        randomNum(); //this moves the program to the next method
            } // END METHOD USERCHOICE
    I think that it goes somewhere after the if statement or that I may have to change it to a do while loop.


    Another issue I am having is comparing the data gathered from the userChoice method to the randomNum / getRand methods.

    Java Code:
    public static void randomNum() //random number generator method 
            {                                      // put in its own method when done
               int counter = 1;
               int getNum;
    
               do
               {
               	 getNum = getRand();
               	 counter++;                           // same as ctr  = ctr + 1;
               } while (counter <= 6); 
               
               System.out.println ("The roll was " + getNum);
               // use do while loop for fun
    
            }// main
    
            public static int getRand()  // returns an int value via the return statement
            {
            	Random generator = new Random();  // declare a new Instance of type Random
    
               int  rNum  = generator.nextInt(6) + 1; //  generator.nextInt(6) creates numbers from 0 to 5;  thus the reason to add 1
    
               return rNum;  // return value
    My apologies if you cannot understand what I am trying to get accomplished. I am very well spoken but not in Java programming.

  2. #2
    subzero1012 is offline Member
    Join Date
    Feb 2012
    Location
    Phoenix, AZ
    Posts
    26
    Rep Power
    0

    Default Re: Multiple issues with my 1-6 guessing game.

    Have you considered passing the getChoice variable to the randomNum method? Then, you can just say
    Java Code:
    if( getChoice == getNum) System.out.println("match found"); 
    else System.out.println("match not found");
    This could be placed in the randomNum() method.

  3. #3
    intEquals_EJ is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Multiple issues with my 1-6 guessing game.

    Java Code:
    int x = 5;
    doStuff(x); // pass a COPY of x (the value 5) to the doStuff method
    
    The doStuff method looks like this:
    
    void doStuff(int y) {
    
       // use y in some way
    }
    A copy of the value in x, which is 5, is passed into the doStuff() method.

    I am trying to do this except "x" equals "getChoice" and there is no equal sign. No value assigned to getChoice. I want to pass a copy of getChoice to the randomNum(); method like you said.


    I have also tried assigning the getChoice variable to itself to no avail.
    Last edited by intEquals_EJ; 03-16-2012 at 07:23 PM.

Similar Threads

  1. guessing game
    By MrM in forum New To Java
    Replies: 1
    Last Post: 02-02-2011, 08:51 PM
  2. Need help in Guessing Game
    By rose in forum Java Gaming
    Replies: 4
    Last Post: 10-27-2010, 11:43 PM
  3. Guessing Game
    By rose in forum Java Gaming
    Replies: 4
    Last Post: 10-27-2010, 09:00 PM
  4. guessing game help
    By yasmin k in forum AWT / Swing
    Replies: 4
    Last Post: 10-31-2009, 06:37 PM
  5. guessing game using GUI
    By yasmin k in forum New To Java
    Replies: 1
    Last Post: 10-26-2009, 01:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •