Results 1 to 7 of 7
  1. #1
    macfrik is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default Help about Guess the Numbers Program in java

    Hey guys,
    I'm new here. Hello. I have a question for you huys. I've been stuck here for more than 2 hours figuring this out.

    Problem

    Create a menu-driven "Guess the Number Game" that allows the player to choose from two options:

    Version 1 - allows unlimited guesses
    Version 2 - allows 5 guesses
    Specifications

    Create a GuessTheNumber class with the following members
    Instance variables
    keyBd : Scanner
    myGuess, numberToGuess, count : Integer
    Methods
    GuessTheNumber( guess : Integer )
    hint: initialize numberToGuess to guess, count to 0, create keyboard Scanner
    showInstructions()
    hint: displays "how to play the game" instructions for each version
    version1()
    hint: Guess3.java attached
    version2()
    hint: Guess4.java attached
    Create a MenuGuess class that uses a menu to test the GuessTheNumber class
    The menu will have the fallowing options
    Instructions
    Version 1
    Version 2
    Exit

    Can anyone help me out here? I am totally confused and desperate. It is not on my textbook and I don't know anywhere else to search on.

    Thank you very much.

  2. #2
    macfrik is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    Anyone knows this? I'm stuck here. Thank you.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    What's specifically your question?

  4. #4
    macfrik is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    Create a menu-driven "Guess the Number Game" that allows the player to choose from two options:

    Version 1 - allows unlimited guesses
    Version 2 - allows 5 guesses

    How to combine the 2 versions and make the GuessTheNumber class? What command did I miss?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Since both versions repeat, this should tell you that you will need loops. In one version, you know the maximum number of times the loop will occur, in the other you don't. That suggests to me that one will use a for loop that has a break statement in it if the game is solved, and the other will use a while loop. Do you know which will use which? Also, it may be easier to put these versions each in their own method and just have your menu call the appropriate method based on the user's selection.

  6. #6
    macfrik is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    import java.util.Random;
    import java.util.Scanner;

    public class GuessTheNumber {

    public static void main(String[] args) {
    //declare variables
    Scanner keyBd = new Scanner( System.in );
    int myGuess, //player guess
    numberToGuess, //random number to guess
    count; //guesses used

    //show introductory message
    System.out.println("\nWelcome to Guess-My-Number\nA GAME of CHANCE and SKILL!");
    System.out.println("You have 5 guesses - Good luck!");

    //pick a random number between 1 and 25
    //Random rNumGen = new Random();
    //numberToGuess = 1 + rNumGen.nextInt(25);
    numberToGuess = 13;

    //give player only 5 tries to guess
    for(count=1; count<=5; count++)
    {
    //get a number from the user
    System.out.print("\nEnter a number between 1 and 25: ");
    myGuess = keyBd.nextInt();

    //good or bad guess??
    if(myGuess < numberToGuess)
    //guess too low
    System.out.printf("\nYour guess [ %d ] was too low...\n", myGuess);
    else if(myGuess > numberToGuess)
    //guess too high
    System.out.printf("\nYour guess [ %d ] was too high...\n", myGuess);
    else
    {
    //good guess
    System.out.printf("\nCongratulations!\nYou guessed [ %d ] in %d %s",
    numberToGuess, count, (count>1)? "tries." : "try.");
    break; //out of for
    }//end else

    System.out.printf("%s", count<5?"Try Again!":"\nSorry, Game Over!");
    }//end for

    if(count > 5)
    {
    //exceeded guesses allowed
    System.out.printf("\nYou had your %d tries.\n", count-1);
    }//end if

    System.out.println();
    }//end main()

    }//end GuessTheNumber


    This is my code. I don't know where to put the loop. Can you give me an idea/ Thank you so much for your reply.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    1) plan the logic of the program on paper before trying to commit it to code. If you do it on paper, you will see for yourself where the loops should go.
    2) Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

Similar Threads

  1. prime numbers program
    By i contra i in forum New To Java
    Replies: 9
    Last Post: 01-15-2009, 08:22 AM
  2. guess number game
    By mistah in forum New To Java
    Replies: 10
    Last Post: 11-23-2008, 04:37 AM
  3. sample of guess high and low game
    By pouria62 in forum AWT / Swing
    Replies: 1
    Last Post: 10-26-2008, 01:57 PM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM
  5. Replies: 0
    Last Post: 03-28-2008, 09:46 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
  •