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 4 of 4

Thread: Guessing Number Game Help

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Guessing Number Game Help

    I do not understand this exercise in programming book for java for starters can someone please show me the code and explain what is going on every line of code what everything does? Create a guessing game that allows a user to guess a number. The program should output "higher" or "lower" depending on each guess.

    1. Document the code to explain what is going on. Try the guessing game. How well does it work? Explain what happens when you enter the following values as a guess:

    13
    a
    4.0
    4.5
    3 (a bunch of spaces followed by a 3)
    -4
    - 4 (a negative sign, followed by a bunch of spaces, followed by a 4)

    Explain why you think you get the results you do for each entry.

    2. Modify the program to remove the "sudden stop" of the two calls to System.exit(0). You can use break or any other technique, but I want the code to leave the while loop as soon as the user wins. I do not want the program to output "You lose" when the user wins.

    3. Move all Scanner input code to the method provided (it is calledgetNumberFromUser() ) and is currently empty of code. You need to add code that does the following:
    a. Check that the input is a integer; and
    b. Ensure that the integer is in the proper range of legal guesses.

    4. Modify the code to add a menu at the beginning of the game. It should have 2 options:
    1. Classic
    2. Custom
    Check if the user choses 1 or 2. If they chose 1, play the game with the default values (1-100, 10 guesses). If they chose 2, ask them to enter 3 values: A low value, a high value, and a number of guesses. For example, if the user enter 4, 10, and 9999, the game would generate a number between 4 and 10 and allow 9999 guesses before the player loses.

    There is a lot of code you can add to ensure "tidy" inputs. For example, you should check that the value entered for High is legal compared to the value entered for Low. Thus, the custom games values of 100, 10, 99 is not legal, since you cannot have a number range of 100 to 10.

    import java.util.Scanner;
    import java.util.Random;
    public class Guess {
    static int lowNumber=1;
    static int highNumber=100;
    static int numOfGuessesAllowed=10;
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Random generator = new Random();
    int target = generator.nextInt(highNumber) + lowNumber;
    System.out.println(target);
    int numOfGuesses=0;
    while(numOfGuesses < numOfGuessesAllowed) {
    //Some of these lines should go into the getNumberFromUser() method
    System.out.println("Guess a number:");
    int guess = sc.nextInt();
    boolean hasWon = checkNumber(guess, target);
    numOfGuesses++;
    if(hasWon) {
    System.out.println("You win");
    System.exit(0);
    }
    }
    System.out.println("You lose");
    System.exit(0);
    }
    public static boolean checkNumber(int guess, int target) {
    if(guess < target) {
    System.out.println("Higher");
    } else if (guess > target) {
    System.out.println("Lower");
    }
    return guess==target;
    }
    //Fill in this method with code to parse input from the user and
    //Ensure it is a number between low and high values.
    public static int getNumberFromUser(Scanner sc) {
     
    }
    }

    this is what i got so far someone please help


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Guessing Number Game Help

    What are you stuck on exactly? What happens when you compile the code?

    Rather than posting all of the requirements you need to take it step by step..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Number Game Help

    :O That's cheating! Does Mr. Forrest know you posted this?

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,630
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Guessing Number Game Help

    Quote Originally Posted by spx96 View Post
    :O That's cheating! Does Mr. Forrest know you posted this?
    Just saying, the simple fact that you would find this post means you must have been googling for a solution, which means you are also cheating. Does Mr. Forrest know that?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Number Array
    By TheJavaGuy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2011, 03:59 AM
  2. Guessing Game begginner question
    By okupa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2010, 07:31 AM
  3. 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
  4. begginer wondering why his guessing game won't work
    By Ligawulf in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 7th, 2010, 11:22 PM
  5. letter to number
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2009, 06:01 AM