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

Thread: Random Numbe rGame: Displaying ranges on console

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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.

    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.
    		}
     
    	}
    }
    Last edited by helloworld922; October 5th, 2012 at 12:34 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    inflames098 (October 5th, 2012)

Similar Threads

  1. My code is not displaying on the console. In need of much help!
    By kvn7 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 13th, 2012, 02:42 PM
  2. How to perform search for a value in table ranges : FROM - TO
    By jsimha in forum Loops & Control Statements
    Replies: 1
    Last Post: August 25th, 2012, 08:41 AM
  3. I need to make cases in a switch statement over certain ranges
    By davis220 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 20th, 2012, 10:14 PM
  4. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM