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.
Code Java:
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
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..
Re: Guessing Number Game Help
:O That's cheating! Does Mr. Forrest know you posted this?
Re: Guessing Number Game Help
Quote:
Originally Posted by
spx96
: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?