Results 21 to 22 of 22
Thread: Think of a number Program
- 11-18-2011, 12:43 AM #21
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: Think of a number Program
thank you all very much for your help on thisJava 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); } } } } }
many thanks Andy.
- 11-18-2011, 08:07 AM #22
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
Similar Threads
-
Help with my first program. Random number gen.
By bobmiknob in forum New To JavaReplies: 1Last Post: 09-25-2011, 12:43 AM -
Program that truncates a number
By scottmulla in forum New To JavaReplies: 1Last Post: 03-25-2011, 09:45 PM -
smallest number java program
By elmo in forum New To JavaReplies: 9Last Post: 11-18-2010, 09:29 PM -
Phone number Program ..
By Sary in forum New To JavaReplies: 9Last Post: 03-17-2010, 07:15 PM -
A Number Converting Program!
By WastedxYears in forum New To JavaReplies: 2Last Post: 01-09-2010, 12:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks