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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with my homework?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Thanked 0 Times in 0 Posts

    Default Help with my homework?

    Hi friends,
    I have been assigned the following task for my begginer java course.

    Write a program that will guess a number that the user thinks up based on the rules below:
    1. You must ask the user to think of an integer between 1 and 1000. They do not type this number into the computer. They just think of it in their head.
    2. The computer will make an initial guess of the number.
    3. The user must indicate if the guess is too high, too low or correct (use whatever input format you want for this, but specify it in the comments and prompt, as usual).
    4. If the computer has not guessed the right number, it should make another guess.
    5. This process goes on until the computer guesses the userís number. If you design your program correctly it should always guess the number in 10 tries or less.
    6. Your program should also indicate how many guesses it took to guess the number.

    Were supposed to limit which methods we use to the ones we learned in class, but basically I know that I need to create a method that generates a random number, then changes the range of random numbers to be between 1 or 1000 depending on user's answer, then continues to do so until the user's number is reached, at which point the method is not repeated and the amount of guesses is returned.
    How do I generate a random number between a given range?

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 827 Times in 770 Posts
    Blog Entries

    Default Re: Help with my homework?

    Quote Originally Posted by mshem View Post
    How do I generate a random number between a given range?
    Either of the two following classes can be used to create random numbers - check out their API's for the appropriate methods.
    Random (Java Platform SE 6)
    Math (Java Platform SE 6)

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Thanked 0 Times in 0 Posts

    Default Re: Help with my homework?

    You are not generating a random number, you are guessing the random number that the user has thought of in their head. Your objective is to get the correct answer within 10 guesses (binary search). I'm thinking of 17. Your program asks if I am thinking of 500. I say, no, lower. You guess 250, 125, 62, 31, 15, 23, 19, 17

Similar Threads

    By jmillernc01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 11:13 AM
  2. Homework help
    By hockey15 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 23rd, 2012, 11:28 PM
  3. I am doing my homework =D
    By valenciajv in forum Java Theory & Questions
    Replies: 9
    Last Post: October 7th, 2011, 04:39 PM
  4. Homework
    By jdonaldson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 10:09 AM
  5. need help with homework!
    By programmer12345 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 27th, 2009, 05:34 AM

Tags for this Thread