Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com help with creating a guessing game

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 10-09-2012, 03:43 AM PM User | #1 sexton325 New to the CF scene   Join Date: Oct 2012 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts help with creating a guessing game Hello. I am working on a homework assignment for my intro java class. The program should generate a random number from 1 to 20, and prompt the user for a guess. If the user guesses wrong, the program prompts the user again for the same number (no new random number). The program should keep asking the user until he/she gets it right. Once the user guesses the right number, the program should display "You won! The number of guesses was ___". Guesses outside the range of 1-20 do not count. I keep trying to make this program work but i am struggling hard. for starters, is this how you generate a random number? [CODE]Random rGen = new Random(); rNum = rGen.nextInt(20) + 1;[CODE]
 10-09-2012, 03:46 AM PM User | #2 sexton325 New to the CF scene   Join Date: Oct 2012 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts my professor gave us this algorithm sheet to help us out.  Declare all needed variables Int user's guess Int random number Int count the valid attempts Scanner class object Random class object  Create a random number and assign it to the variable  Begin loop(while user's guess !=random number) Prompt for the user's guess Compare user's guess to the random number If (user's guess equals random number) Output “You won!” Else Output “Try again.” but i dont understand why you would begin the loop before prompting for the user's guess. should i follow this? or am i just not getting it?
 10-09-2012, 03:19 PM PM User | #3 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,591 Thanks: 4 Thanked 2,597 Times in 2,566 Posts For your first question, Random can be used to get an inclusive to exclusive random number. So that would give between 1 and 20. For your second question, its for convenience. If you don't do it, then you need to write the code to handle the input twice, the first time to determine if the loop should be entered, and then again to take input if the answer is incorrect. Putting it within the loop prevents needing two prompts.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 09:10 AM.