Results 1 to 6 of 6

Thread: guess # program

  1. #1
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default guess # program

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    import java.io.*;         //needed for file and IOException
    
    /**
    	This program will demonstrate a guessing game.
    	
    */
    
    public class RandomNumberGuessingGame
    {
    	public static void main(String[] args) throws IOException
    	{
    		// Constant for maximum random number
    		final int MAX_NUMBER = 10;
    		
    		// Variables go here
    		int guess; 			// Holds the users guess
    		int randNum;		// Holds the random number
          int count = 1;    // Number of guess counter
          int guessScore;   // Guess %
          String fileName;  // Name of the file
    		
    		// Create the Scanner object
    		Scanner keyboard = new Scanner(System.in);
    		
    		// Create a random object
    		Random rand = new Random();
    		
    		// Generate random number
      
    		randNum = rand.nextInt(MAX_NUMBER);
    		
    		// Get the users guess
    		System.out.println("I'm thinking of a number.");
    		System.out.println("Guess what it is: ");
    		guess = keyboard.nextInt();
          
          // Make random number is greater than 0
          
         
    		// Respond to the user's guesses
    		while (guess != randNum)
    		{
           
    			if (guess < randNum)
    			{
    				System.out.println("No, that's too low!");
                count++;
    			}
    			else if (guess > randNum)
    			{
    				System.out.println("No, that's too high!");
                count++;
    			}
    			System.out.println("Guess again: ");
    			guess = keyboard.nextInt();
             
            
    		}
    		
    		//Congratuation the user
    		System.out.println("Congratuation!	You guessed it!");
    		System.out.println("I was thinking of the number " + randNum + ".");
          
    
          
          keyboard.nextLine(); //Terminate input buff.
          
          //Get the file name
          System.out.print("Enter the file name: ");
          fileName = keyboard.nextLine();
          
          //Get output file name
          PrintWriter outputFile = new PrintWriter(fileName);
          
          //Calculate Guess Score
          guessScore = ((11-count)/11)*100;
          
          outputFile.println("Game Results");
          outputFile.println("----------------------");
          outputFile.println("Random Number: " + randNum);
          outputFile.println("Number of Guesses: " + count);
          outputFile.println("Guessing Score: " + guessScore + "%");
    		outputFile.close(); //close output
    	
    	
    	
    	}
    }
    ok. edited

    now I have two problems.
    1. I don't know how to make random number always greater than 0
    2. when I calculate total score, i always get 0%. why is that? I tried to other division calculation but jgrasp always shows 0 if number is less than 1. for example if i do 10/5, it shows 2 which is right. but if I do 5/10, instead of shoing 0.5, it always shows 0. how do I fix this?

    pseudo code
    1. computer picks random number between 1 to 10
    2. user input guess #
    3. computer asks number over and over until user gets right one
    4. display result

    Game Results
    ---------------------------------
    Random Number: (display answer)
    Number of guesses: (display answer)
    Guessing Score: (% accuracy guessing) - Feedback Message
    Last edited by shin777; 10-30-2013 at 08:33 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: guess # program

    A counter is just a variable that increments by one per guess. So that should be pretty easy compared to the rest of your program.

    A random number that is between 1 and 10 inclusive. Have you tried to figure this out on paper? First, look at way you are generating the number and
    see if you can make a range, any range, out of that. Then apply the technique to your current program.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: guess # program

    I figured out counter but not random number value being always greater than 0 and my guess score calculation always shows 0. why is java show 0 if division result is less than 1? how do i fix this?
    I updated my code.
    Last edited by shin777; 10-30-2013 at 08:25 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: guess # program

    Okay, here's a hint for the random number problem.

    If I am guaranteed to always get a number between 0 and 10 inclusive. Then how to I make it between 1 and 11 inclusive?

    Regards,
    Jim
    Last edited by jim829; 10-30-2013 at 09:49 PM. Reason: clarifed post
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default Re: guess # program

    why is java show 0 if division result is less than 1?
    Because you declare this score number as an int. And int doesn't have decimal places, it rather truncate it..

  6. #6
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: guess # program

    Quote Originally Posted by milovan View Post
    Because you declare this score number as an int. And int doesn't have decimal places, it rather truncate it..
    oh.. never mind. it works when I changed to double too.

Similar Threads

  1. can you guess the output of this program?
    By killutch in forum New To Java
    Replies: 4
    Last Post: 09-18-2012, 09:39 AM
  2. guess game GUI
    By tomandhisjones in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 06:30 AM
  3. Easy Thing I Guess ...
    By loopsnhoops in forum New To Java
    Replies: 1
    Last Post: 02-24-2011, 10:37 PM
  4. Guess Character
    By pinkdreammsss in forum Java Applets
    Replies: 14
    Last Post: 05-05-2010, 07:28 PM
  5. Help about Guess the Numbers Program in java
    By macfrik in forum New To Java
    Replies: 6
    Last Post: 03-25-2009, 03:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •