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 assignment

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

    Default guessing game assignment

    here is the assignment:
    Program 6.2

    Write a program named GuessingGame that generates a random number in the range of 1 through 100 and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." If the user guesses the correct number, the application should congratulate the user and then generate a new random number so the game can start over. Keep a count of the number of guesses that the user makes. When the user correctly guesses the random number, the program should display the number of guesses it took. When the user wins the game, ask if he/she wants to play again. If the answer is yes, then generate a new random number and start over.

    For this program, create the following functions:
    •A function to generate and return a random number in the range of 1 through 100.
    •A function that asks the user for a guess and returns the value.
    •A function that evaluates the user's guess and returns a message indicating whether it is too high, too low, or a match.

    Your program should have a game loop that continues as long as the user wants to keep playing. Within this outer loop will be an inner loop responsible to keep asking the user for guesses until the user gets the number right. In these loops call the appropriate functions you've created as needed.



    I have tried to do this program and i like to think that ive gotten pretty far i just cant figure out the right order or loop or something help me...here is my code:

    import java.util.Scanner;
    import java.util.Random;
    public class GuessingGame
    {
    public static void main(String[] args)
    {
    int number;
    number = GetNumber(5);
    int numberOfTries;
    numberOfTries = GetTries(5);
    int guess;
    guess = GetGuess(5);
    }

    public static int GetNumber(int number)
    {
    Random rand = new Random();
    number = rand.nextInt(100) + 1;
    return number;
    }

    public static int GetGuess(int guess)
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Guess a number between 1 and 100: ");
    guess = input.nextInt();
    return guess;
    }

    public static int GetTries(int numberOfTries)
    {
    int number;
    number = GetNumber(5);
    int guess;
    guess = GetGuess(5);
    numberOfTries = 0;
    if( guess > number)
    {
    System.out.println("Too high. Try again.");
    numberOfTries++;
    guess = GetGuess(5);
    }
    if( guess < number)
    {
    System.out.println("Too low. Try again.");
    numberOfTries++;
    guess = GetGuess(5);
    }
    if( guess == number)
    {
    System.out.println("Congratulations you win!");
    System.out.println("The number was " + number);
    System.out.println("It took you " + numberOfTries + " tries.");
    }
    return numberOfTries;
    }

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: guessing game assignment

    cant figure out the right order or loop or something
    Please explain what the problem is. Copy and post the program's output and explain what is wrong with what the program is doing. Show what the output should be.

  3. #3
    Member
    Join Date
    Feb 2011
    Posts
    33
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: guessing game assignment

    I'm not sure what your programming level is, but this would be much easier to do with a while loop.


    All intelligent thoughts have already been thought;
    what is necessary is only to try to think them again.



Similar Threads

  1. Guessing Game
    By scottey93 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 7th, 2011, 02:50 PM
  2. Reverse Number guessing game
    By rarman555 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 23rd, 2011, 10:39 PM
  3. Number Guessing Game
    By JayK in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 16th, 2011, 09:46 PM
  4. Guessing Number Game Help
    By Shadow20 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2011, 12:41 PM
  5. Guessing Game begginner question
    By okupa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2010, 07:31 AM