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;
}
}