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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: guess program problems.

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Thanked 0 Times in 0 Posts

    Default guess program problems.

    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
          double 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!");
    			else if (guess > randNum)
    				System.out.println("No, that's too high!");
    			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("Random Number: " + randNum);
          outputFile.println("Number of Guesses: " + count);
          outputFile.println("Guessing Score: " + guessScore + "%");
    		outputFile.close(); //close output

    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

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 481 Times in 472 Posts

    Default Re: guess program problems.

    Your problems:

    1. If you need random numbers of a certain size, then either start with what you have and add, multiply, or use some combination of both to get a random number of the desired size.

    2. Integer types can't be used to calculate, store, or display decimal values by themselves. Java 'assumes' a value is an integer unless told otherwise, so the equation, (((11-count)/11)*100), is all integers.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 1,974 Times in 1,948 Posts

    Default Re: guess program problems.

    5/10, instead of shoing 0.5, it always shows 0.
    That is how integer math works. An int can NOT hold a value of 0.5
    Make the expression and variables double if you want values like 0.5
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Hangman Program Java: Comparing the User's Guess with Another String Letter by Letter
    By theonlydvr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 29th, 2013, 05:35 PM
  2. Guess the error in the code
    By anek77713 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 10th, 2013, 04:53 AM
  3. Word Guess program with a Null Pointer Exception
    By Wallatrix in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2013, 02:42 AM
  4. Please help to understand whats the wrong with my guess. - Simple program
    By rayan2004 in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2012, 12:03 AM
  5. Help with 3 guess Dice game
    By tabmanmatt in forum Loops & Control Statements
    Replies: 1
    Last Post: November 28th, 2012, 06:42 PM