# Random Numbe rGame: Displaying ranges on console

Printable View

• October 5th, 2012, 01:31 PM
inflames098
Random Numbe rGame: Displaying ranges on console
Hey folks. Beginner Java user and first time on the forum. I have a BS in microbiology from Cal Poly Tech, but after 3 years in the biotech industry, decided to pursue a CS degree. Great opportunity since I can complete in one year through Oregon State's accelerated one year degree since I already have a BS.

Anyway, enough of me, lets get to the good stuff. So, I created a random number game where the user guesses a number (my range is from 1-52) and the cpu tells the user if they are correct, too high, or too low. Simple enough.

For extra credit in my class, (now I won't be angry if this doesn't get solved, but I'm curious to know about how to code this), after a user guesses a number, it updates to the prompt to show a range that the user is wrong, but that they are close to the number. For example:

console displays: pick a number from 1-52. User guesses: 26
console displays: Sorry, that is too low. pick a number from 27-52. User guesses: 40
console displays: Sorry, that number is too high. Pick a number from 27-39
.....
user guesses correctly: 32
console displays: That is correct.

Plus, the user is allowed to play the game repeatedly without having to quit and restart the program.

I understand that this will be an if/else statement, but I'm having a hard time writing the code and telling the JAVA what I want. I also understand that I should use high and low range in the if statements and that the program (if written correctly) will add the half to the low range and/or subtract the half from the high range. Not sure how to display this.

I will display my code with the random number game, if you guys want to critique it. I just need some more guidance for this Extra Credit question. Appreciate any help! Thanks.

Code java:

```import java.util.Scanner; // Taking user import.       public class GuessMyCard {   public static void main(String[] args) { System.out.println("Welcome to 'Guess The Number!!!!!!'"); // Special instructions for the game. System.out.println(); System.out.println("How to Play:" ); System.out.println("(1) You have 5 guesses to pick a number from 1 to 52!"); System.out.println("(2) If you guess right, you win a big prize!"); System.out.println("(3) If you guess wrong, we'll shoot you out of a cannon!"); System.out.println(); System.out.println("Enter your guess: "); Scanner scanner = new Scanner(System.in);   int randomNumber = (int)Math.ceil(Math.random() * 52) + 1; // Math.random helps generate a random integer. Adding 1 makes sure the range is from 1-52 and not starting from 0. int numberofGuesses = 5; // Math.ceil helps round the integer to a whole number. ([url=http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html]Math (Java 2 Platform SE v1.4.2)[/url]). int userGuess = 0; // 5 is set to give the user 5 guesses.   while(numberofGuesses > 0) // Loop is declared. { userGuess = scanner.nextInt();   if (userGuess == randomNumber) { // This statement means that if user guesses the random number, then the game is over. System.out.println("YOU HAVE WON USER!!! AND YOUR PRIZE IS THE SATISFACTION OF GUESSING A RANDOM NUMBER CORRECTLY!!!"); break; // Game stops when the user guesses the number correctly.   } else if (numberofGuesses == 1) { // Loop will restart if user is wrong, until guesses are up. Make the guess start at 1, not 0. 5 tries are given until the statement is executed. System.out.println("YOU LOSE!!! THE CORRECT ANSWER WAS " + randomNumber + ". TIME FOR CANNON SHOOTING!!!");   } else if (userGuess > randomNumber) { System.out.println("Too high man!"); // If user guesses too high, this statement executes.   } else if(userGuess < randomNumber) { System.out.println("Too low man!"); // If user guesses too low, this statement executes.   } numberofGuesses--; // Decreases the number of guesses. }   } }```
• October 5th, 2012, 01:46 PM
helloworld922
Re: Random Numbe rGame: Displaying ranges on console
One thing I've noticed many new programmers (and a surprising number of "experienced" ones, too) under-utilize are helper methods.

A hint for making it easier to organize your code:

1. Create a method which plays the game once.
2. In you main method, have a while loop which calls the play game once method. After the method returns (the game is over), ask the user if they want to keep playing. If so, keep looping. Otherwise, leave the loop and allow your program to end.

As far as the extra-credit portion goes, think about what numbers the user can choose after making a guess.

Ex.
The original maximum bounds are min=1 and max=52 (inclusive).

I guess 30

The game tells me my number is too low. Naturally, I want to guess numbers larger than 30 since it would be silly to guess any number lower or guess the same number again. So I should set the min to be 31. The max remains unchanged and is still at 52.

Now I guess 40

The game tells my number is too high. Again, it would be silly to guess any number higher than 40 or to guess 40 again. So I should set my max to be 39. The min remains unchanged and is still at 31.

Think about how you can go about putting this concept into code.