Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22
  1. #21
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: Think of a number Program

    Java Code:
    import java.util.*;
    public class ThinkNumber
    {
    
    public static void main(String [] args)
    {
    	Scanner myKeyboard = new Scanner(System.in);
    	{
    		
    		int randomNumber = (int)(Math.random() * 100) + 1;
    	
    		System.out.print("Guess a number between 1 and 100: ");
    	{
    			
    		int guess = myKeyboard.nextInt();
    		int attempt = 1;
    		
    		
    		while(guess != randomNumber)
    	{
    			
    		if(guess < randomNumber)
    	{
    		System.out.println("Too Low");
    	        System.out.println("Please Guess Again");
    	}
    		else if(guess > randomNumber)
    	{	
    		System.out.println("Too High");
    		System.out.println("Please Guess Again");
    	}
    		guess = myKeyboard.nextInt();
    		attempt++;
    		}
    			{
    				System.out.println("Correct! You win!");
    				System.out.print("Your Number Of Attempts Was " + attempt);
    			       }
    		        }
    	     }
    				
          }
    }
    thank you all very much for your help on this

    many thanks Andy.

  2. #22
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Think of a number Program

    I'm glad you've got it sorted out.

    Don't go overboard with the braces. Basically, use a pair of braces with

    *if
    *else
    *while
    *for

    and one pair with each

    *class
    *method

    But not otherwise (or you may cause things to break). Also it's a good idea to indent the lines that go between braces. nested braces: more indent.

    [edit] I realise you probably don't want to muck around with code that does what it's supposed to! But you can always revert back if anything goes wrong, or ask here.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Help with my first program. Random number gen.
    By bobmiknob in forum New To Java
    Replies: 1
    Last Post: 09-25-2011, 01:43 AM
  2. Program that truncates a number
    By scottmulla in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 10:45 PM
  3. smallest number java program
    By elmo in forum New To Java
    Replies: 9
    Last Post: 11-18-2010, 10:29 PM
  4. Phone number Program ..
    By Sary in forum New To Java
    Replies: 9
    Last Post: 03-17-2010, 08:15 PM
  5. A Number Converting Program!
    By WastedxYears in forum New To Java
    Replies: 2
    Last Post: 01-09-2010, 01:47 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
  •