Quantcast

Maximum PC

It is currently Sun Dec 21, 2014 9:33 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Java Guess My Number Program Help
PostPosted: Sun Nov 22, 2009 10:28 pm 
8086
8086
User avatar

Joined: Thu Feb 19, 2009 1:57 pm
Posts: 22
I am working on a program and the goal is this:
Generate a random positive 2 digit number.

Give the user up to five chances to guess the right number. If they are wrong, tell them whether their guess was too high or too low. If they guess correctly, tell them that they won and exit the program.

I know I need to use the random class to generate the number and either JOptionPane or a scanner to get the input. Along with a loop...

Can someone help me with this basic program?


Top
  Profile  
 
 Post subject:
PostPosted: Sun Nov 22, 2009 11:42 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24238
Location: Granite Heaven
We don't do homework here.

If you want help, you need to show what you've done and what you need help with .. simply posting the assignment is not going to get it done.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 15, 2009 8:15 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Mon Jan 08, 2007 1:41 pm
Posts: 2033
well, I can get your brain working awhile, right?

it's not our job to enforce academic honesty, but we could at least offer some help.

asking the user to guess the number repeatedly should jump out as you as the perfect place for a loop, such as one that iterates 5 times.

inside the loop, you'd want to ask the user for input then decide what to do based on the input. I think you outlined that part pretty well yourself.

and google is very useful for figuring out which java functions to use for simple integer input and random number generation.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 16, 2009 10:36 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24238
Location: Granite Heaven
I'm pretty sure the assignment was due over a month ago. ;)


Top
  Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2010 10:19 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Jipstyle wrote:
I'm pretty sure the assignment was due over a month ago. ;)


In that case... ;)

Code:
CL-USER>
(defun number-game (num-guesses)
  (let ((num (+ (random 90) 10)))
    (dotimes (i num-guesses)
      (format t "~%You have ~a chances left to guess the number.~%" (- num-guesses i))
      (format t "Guess? ")
      (setq guess (read))
      (cond ((< guess num)
        (format t "You guessed too low!~%"))
       ((> guess num)
        (format t "You guessed too high!~%"))
       (t
        (format t "You guessed correctly!~%")
        (return t))))))
NUMBER-GAME
CL-USER> (number-game 7)

You have 7 chances left to guess the number.
Guess? 50
You guessed too low!

You have 6 chances left to guess the number.
Guess? 75
You guessed too high!

You have 5 chances left to guess the number.
Guess? 67
You guessed too low!

You have 4 chances left to guess the number.
Guess? 71
You guessed too low!

You have 3 chances left to guess the number.
Guess? 73
You guessed too low!

You have 2 chances left to guess the number.
Guess? 74
You guessed correctly!
T
CL-USER>


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.