Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

"HiLo" number guessing game not working - Help please!

814149Nov 18 2010 — edited Nov 18 2010
My game is compiling and read's right (in my head) but doesn't appear to work, any help is highly appreciated, thank you.

Source code. More specifically, I think that it's properly getting the random number, but the guess prompt is not appearing, probably because of my while(random!=number) line?
import javax.swing.JOptionPane;
import java.util.Random;
/**
 * High-Low (HiLo) game.
 *
 * @author 
 * @version 11/18/2010
 */
public class HiLo
{
    String randomNumber = "";
    int random;
    String userNum = "";
    int number;
    /**
     * Asks player if he wants to play, gets random number, gets user guess, checks the users guess, asks to repeat.
     *
     */
    public void play()
    {
        getRandom();
        while(random!=number)
        {
            getGuess();
            checkGuess();
        }
    }

    /**
     * Gets the users guess.
     */
    public int getGuess()
    {
        userNum = JOptionPane.showInputDialog ("Please Guess the number");
        int number = Integer.parseInt(userNum);
        return number;

    }

    /**
     * Gets a random number between 0 and 100
     * int named random
     */
    public int getRandom()
    {
        Random randomNumber = new Random();
        int random = randomNumber.nextInt(101);
        return random;
    }

    /**
     * Checks to see if the user's guess is an integer, between 0 and 100, and returns if
     * they're guess is too high or too low.
     */
    public void checkGuess()
    {
        if (number==random)
            JOptionPane.showMessageDialog(null, "You Win!");
        else if (number<random)
            JOptionPane.showMessageDialog(null, "Too low, guess again!");
        else if (number>random)
            JOptionPane.showMessageDialog(null, "Too high, guess again!");
    }
}
Edited by: 811146 on Nov 18, 2010 3:11 PM

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 16 2010
Added on Nov 18 2010
9 comments
1,255 views