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

Thread: Guessing game

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Guessing game

    Is there actually a specific thread to post your games? This is my first guess, and I'm not here to show off or I'll be fooling myself here as it's not the best game. I'm actually just asking for any general advice on my code such as better structuring, etc (I do know about classes but the code is short so it's pointless).

    import javax.swing.JOptionPane;
    import java.util.Random;
     
    public class Window {
    	public static void main(String[] args)
    	{
    		//hold data
    		Random rnd = new Random();
    		int RandomNumber = rnd.nextInt(1001);
    		int low = 0, high = 1000;
    		int Number = 0;
     
    		String GetText;
     
    		// main loop
    		do
    		{
    			GetText = JOptionPane.showInputDialog("Guess the number(" + low + "-" + high + "):");
     
    			Number = Integer.parseInt(GetText);
     
    			if (Number > RandomNumber)
    			{
    				JOptionPane.showMessageDialog(null, "Lower!", "Guess The Number!", JOptionPane.PLAIN_MESSAGE);
    				high = Number;
    			}
    			else if (Number < RandomNumber)
    			{
    				JOptionPane.showMessageDialog(null, "Higher!", "Guess The Number!", JOptionPane.PLAIN_MESSAGE);
    				low = Number;
    			}
    		} while (Number > RandomNumber || Number < RandomNumber);
     
    		JOptionPane.showMessageDialog(null, "You are a winner", "Congratulations!!!", JOptionPane.PLAIN_MESSAGE);
    	}
    }

    - Nicky


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,326
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: Guessing game

    Comments:
    Window and Number are the names of a Java SE classes. It'd be better to use other names
    Java naming conventions are that variable names start with lowercase letters.
    There isn't a test for when the numbers are equal. That could be handled by a comment saying the loop exits when they are equal
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing game

    Quote Originally Posted by Norm View Post
    Comments:
    Window and Number are the names of a Java SE classes. It'd be better to use other names
    Java naming conventions are that variable names start with lowercase letters.
    Thanks for the suggestions, I will keep them in mind.

    There isn't a test for when the numbers are equal. That could be handled by a comment saying the loop exits when they are equal
    while (Number > RandomNumber || Number < RandomNumber)

    If the number isn't higher or lower then it must be equals that's when it finishes and shows the congratulations message.

    - Nicky

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,326
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: Guessing game

    A comment makes the programmer's intentions clearer.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Guessing game
    By namenamename in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2013, 05:47 PM
  2. Guessing Game
    By loreneli113 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 11th, 2013, 06:44 PM
  3. Guessing game help
    By Norm in forum Loops & Control Statements
    Replies: 4
    Last Post: March 23rd, 2013, 07:54 AM
  4. guessing game
    By scottey313 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2011, 01:30 PM
  5. Guessing Game
    By scottey93 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 7th, 2011, 01:50 PM