• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with assignment guessing the number game!  RSS feed

micah wise
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there folks! New java coder here, who is in need of help since I am lost! Ok so this was a 2 part assignment and I got the first part ok; part 1 is for me to create a program where the computer asks the end user to input a number between 1 and 100, if it is wrong it spits back either too low or too high or it is a match. He also wanted a counter and with a little help from you folks I got that with little trouble. Now, on this 2nd part I am a bit baffled, I sort of get it but am mostly in the dark lost. On the 2nd part he wants:

Modify the program so that the random number can be chosen between any range of numbers.
Allow the user to pick the range.
Then the computer will pick a secret random number in this range.
The user should then randomly guess the secret number that was chosen.
Prompt the user to continue guessing until there is a match with the random number selected by the computer.
Tell the user (nicely) if the guess was correct, too high, or too low.
Count the number of guesses required to find a match.

This is the code I have for the 1st part, as I mentioned I am sort of getting what he's asking for but mostly lost in the dark...any help, any help, direction, thoughts, opinions, etc at all would be most appreciated!!!

fred rosenberger
lowercase baba
Posts: 12563
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you refactor this. All your code is in a single method - and that's not the best way to design it.

Here's why...

If instead you had created methods like

Then to make the changes requested, you can focus on what's different new, and not worry at all about what doesn't need to change. for example. isValueInRange might take three parameters: a low, a high, and a value.  It could test if value is between low and high. 

Once you have that method, it doesn't matter any more how you get low, high, and value...so you can re-work the "get info" part, and you know your isValueInRange is ready to use.
Carey Brown
Saloon Keeper
Posts: 3310
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uses the old ramdomizing utilities. Change it to

Now it may be a bit clearer to you where in your program you'll need to replace occurrences of '100'.
Junilu Lacar
Posts: 11477
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pay attention to Carey's suggestion about using Random.

You need to figure out a generally applicable formula that you can use. Kind of like using the formula (Area = 0.5 * base * height) to find the area of a triangle.  You'll do something similar for your random number generation. First, identify what values can change. Then use variables to represent those changeable values instead of hardcoding specific values like 1 and 100. Then work out the math and the formula that will give you the desired results when you plug in the variable values.

It's easier to figure out a formula when you compare a few different examples:

Do you see a pattern? From this, you should be able to figure out a formula for generating a random number that falls within a specified range defined by a lower and upper bound.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!