Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Guessing Game begginner question

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Calling Methods question

    Hi guys,

    This is my first thread.

    I have the following problem to resolve. I was able to do it within one method, but the requirements are different.

    The program should pick a random number between one and ten (inclusive) and prompt the user for their guess. For each guess made, the program should tell the user if the guess was too high, too low, or correct. The user should only have four (4) tries to get it right before they lose the game.

    When a game is over, a dialog should announce if they won or lost and ask the user if they want to play again. Your dialog should have yes and no buttons. If they lost this dialog box must show them what the correct answer was.

    Requirements :
    - All user input and output should be done using javax.swing.JOptionpane class
    - The program should use the class java.util.Random to generate the numbers
    - Your code must have the following methods at least:
    public static void main ( String [] args ) This method is responsible for the "Play again?" logic, including the you won/you lost dialog. This means a loop that runs at least once, and continues until the player quits.
    static boolean playGame ( ??? ) This method is responsible for playing one complete game each time it is called, including the what is your guess? input dialog. Note the message displayed in the input dialog changes each time through the loop, to show if the user's last guess was too high or too low. The method should return true if the user won. If they don't guess correctly after four tries, the user has lost and the method should return false.
    static int compareTo ( ???, ??? ) This method compares the user input (a single guess) with the correct answer. It returns a negative integer if the guess is too low, a positive integer if the guess is too high, and 0 (zero) if the guess is correct.

    So far I am stuck with this code:

    import javax.swing.*;
    import java.util.*;
     
    public class GuessingGame
    {
        public static void main (String [] args)
        {
            //This method is responsible for the "Play again?" logic, including the you won / you lost dialog
            //This means a loop that runs at least once, and continues until the player quits
            boolean x =false;
            boolean win = false;
            do
            {
                playGame(x);
            }
            while (win == false);
            JOptionPane.showInternalConfirmDialog(null,"please choose one", "information",JOptionPane.YES_NO_CANCEL_OPTION, 
     
    JOptionPane.INFORMATION_MESSAGE);
        }
        public static boolean playGame (boolean choice)
        {
            //This method is responsible for playing one complete game each time it is called, including what's your guess
            //input dialog
            final int TOTALGUESS = 4; //Number of total guesses the user has.
            int guessCount = 0; //Number of guesses the user has made
            int rndNumber; // A random number picked by the computer.
            Random rndNumbers = new Random();    //Generate random number.
            rndNumber = rndNumbers.nextInt(10);
            //Create input dialog
            while (guessCount <= TOTALGUESS)
            {
    	// A number entered by user as a guess.
            String guess = JOptionPane.showInputDialog (null, "What's your guess? (1-10)", "Guessing Game", JOptionPane.QUESTION_MESSAGE); 
            // From string to int
            int intGuess = Integer.parseInt(guess);
                compareTo (intGuess,  rndNumber); //Call method to compare guess with random.
                guessCount++;
            }
            return choice;
        }
        public static int compareTo (int num1, int num2) 
        {
            String message;
            //This method compare the user input with the correct answer
            if (num1 == num2) {
                message = JOptionPane.showInputDialog(null, "Well done! Your choice was right!", "Guessing Game",JOptionPane.QUESTION_MESSAGE);
    	    return 0;
    	    break;		 
     
            } 
            else if (num1 > num2) 
            {
                message = JOptionPane.showInputDialog(null, "Sorry too high! Try again", "Guessing Game", JOptionPane.QUESTION_MESSAGE);
    	    return 1;	
            } 
            else  
            {
                message = JOptionPane.showInputDialog(null, "Sorry too low! Try again", "Guessing Game", JOptionPane.QUESTION_MESSAGE);
                return -1;
            } 
            return num2;
     
        }
    }

    Can someone please help me with this ?
    It seems I am quite confuse with the way how the methods are called.

    Thanks in advance!
    Okupa
    Last edited by okupa; October 19th, 2010 at 09:05 AM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Calling Methods question

    Quote Originally Posted by okupa View Post
    Hi guys,

    This is my first thread.

    I have the following problem to resolve. I was able to do it within one method, but the requirements are different.

    The program should pick a random number between one and ten (inclusive) and prompt the user for their guess. For each guess made, the program should tell the user if the guess was too high, too low, or correct. The user should only have four (4) tries to get it right before they lose the game.

    When a game is over, a dialog should announce if they won or lost and ask the user if they want to play again. Your dialog should have yes and no buttons. If they lost this dialog box must show them what the correct answer was.

    Requirements :
    - All user input and output should be done using javax.swing.JOptionpane class
    - The program should use the class java.util.Random to generate the numbers
    - Your code must have the following methods at least:
    public static void main ( String [] args ) This method is responsible for the "Play again?" logic, including the you won/you lost dialog. This means a loop that runs at least once, and continues until the player quits.
    static boolean playGame ( ??? ) This method is responsible for playing one complete game each time it is called, including the what is your guess? input dialog. Note the message displayed in the input dialog changes each time through the loop, to show if the user's last guess was too high or too low. The method should return true if the user won. If they don't guess correctly after four tries, the user has lost and the method should return false.
    static int compareTo ( ???, ??? ) This method compares the user input (a single guess) with the correct answer. It returns a negative integer if the guess is too low, a positive integer if the guess is too high, and 0 (zero) if the guess is correct.

    So far I am stuck with this code:

    import javax.swing.*;
    import java.util.*;
     
    public class GuessingGame
    {
        public static void main (String [] args)
        {
            //This method is responsible for the "Play again?" logic, including the you won / you lost dialog
            //This means a loop that runs at least once, and continues until the player quits
            boolean x =false;
            boolean win = false;
            do
            {
                playGame(x);
            }
            while (win == false);
            JOptionPane.showInternalConfirmDialog(null,"please choose one", "information",JOptionPane.YES_NO_CANCEL_OPTION, 
     
    JOptionPane.INFORMATION_MESSAGE);
        }
        public static boolean playGame (boolean choice)
        {
            //This method is responsible for playing one complete game each time it is called, including what's your guess
            //input dialog
            final int TOTALGUESS = 4; //Number of total guesses the user has.
            int guessCount = 0; //Number of guesses the user has made
            int rndNumber; // A random number picked by the computer.
            Random rndNumbers = new Random();    //Generate random number.
            rndNumber = rndNumbers.nextInt(10);
            //Create input dialog
            while (guessCount <= TOTALGUESS)
            {
    	// A number entered by user as a guess.
            String guess = JOptionPane.showInputDialog (null, "What's your guess? (1-10)", "Guessing Game", JOptionPane.QUESTION_MESSAGE); 
            // From string to int
            int intGuess = Integer.parseInt(guess);
                compareTo (intGuess,  rndNumber); //Call method to compare guess with random.
                guessCount++;
            }
            return choice;
        }
        public static int compareTo (int num1, int num2) 
        {
            String message;
            //This method compare the user input with the correct answer
            if (num1 == num2) {
                message = JOptionPane.showInputDialog(null, "Well done! Your choice was right!", "Guessing Game",JOptionPane.QUESTION_MESSAGE);
    	    return 0;
    	    break;		 
     
            } 
            else if (num1 > num2) 
            {
                message = JOptionPane.showInputDialog(null, "Sorry too high! Try again", "Guessing Game", JOptionPane.QUESTION_MESSAGE);
    	    return 1;	
            } 
            else  
            {
                message = JOptionPane.showInputDialog(null, "Sorry too low! Try again", "Guessing Game", JOptionPane.QUESTION_MESSAGE);
                return -1;
            } 
            return num2;
     
        }
    }

    Can someone please help me with this ?
    It seems I am quite confuse with the way how the methods are called.

    Thanks in advance!
    Okupa
    Integer class already has a compareTo() method.

    int compareTo(Integer anotherInteger)
    Compares two Integer objects numerically.

    rndNumber = rndNumbers.nextInt(10);

    Don't know Random class. Know of Math.random() which returns a random number.

    perhaps the statement is adding 10 to it, or resetting it to 10, not sure. You'd have to tell me on that one.

    javax.swing.JOptionPane may have to be imported in full.

    playGame will always return false I think. Nothing changes the value, and x is set in main to false.

    You know your break statement will never be reached as your return statement ends the method and returns the value.

    You could use Integer.compareTo(Integer anotherInteger) which returns a 0 for equal, a 1 for greater than, and a -1 for less than. Also, it'll always be a loss unless you set it to have

    Integer int1, int2;

    if (int1.compareTo(int2) == 0)
    win = true;

    However, your play game always has to have a win variable, or else it'll be confused.

    A do...while loops always executes at least once.

    For instance, I think it'll do this:

    win = false;

    continue;

    go back and try again in while loop

    win becomes true;

    it executes the statement in do.

    It sees that win is now true and so exits the loop.

    Ah...choice is the same as win...or it'll do.

    However, win is false, so choice will be false too.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game begginner question

    Thanks for the help!

Similar Threads

  1. im in a hurry!!Help with a programm..java game of guessing a word
    By mr_doctor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:17 AM
  2. Need help with a third ball in game.
    By vlan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 12th, 2010, 03:35 PM
  3. begginer wondering why his guessing game won't work
    By Ligawulf in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 8th, 2010, 12:22 AM
  4. Game 3x3
    By Koren3 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:43 PM
  5. Job offers to program Hobo Wars
    By MooncakeZ in forum Paid Java Projects
    Replies: 7
    Last Post: September 17th, 2009, 09:41 PM