This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes generating random comments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "generating random comments" Watch "generating random comments" New topic
Author

generating random comments

J Calabrese
Greenhorn

Joined: Jun 10, 2008
Posts: 7
I'm finishing up a project and I just can't get it done fully. We are generating two random numbers, then asking the user to input their guess on the product of the two numbers. If it is correct, it is supposed to show up a random "good" comment (which I have listed). There are 4 good comments. If the answer is wrong, it is supposed to show up a random "bad" comment, which I also have already. There are 3 bad comments.

I have everything correct in my program, but I don't know how to generate a random comment, or as I should say, string.

So let me give you an example:

The 2 random numbers would be 2 and 4.

The user inputs 8 as the answer.

So it is supposed to call the printGoodComment method and give back a random good comment from this list:

good job
excellent
terrific
nice work

So how would I go about randomizing those strings so each one is given randomly when an answer is correct?

EDIT: I've been given the hint to use Math.random() and switch, but I don't know where to start.
[ June 10, 2008: Message edited by: poopshakes ]
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
Take a look at how you're storing the messages. Are they in an array? If so, can you think of a way to use a random number to index the array?
J Calabrese
Greenhorn

Joined: Jun 10, 2008
Posts: 7
We are just now learning arrays, so I don't feel comfortable with them yet.

But I thought of a way to do it.

Since there are 4 random comments possible, I can randomly generate an int number from 1-4. From there, I can use a switch statement to print out the random comment from the random number that was generated. Wouldn't that work?
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

I've been given the hint to use Math.random() and switch, but I don't know where to start.


Your hint is clear to get your required solution.

I personally suggest you to use Random.nextInt(int) instead of Math.random() as it return double value which is more complex to handle

With Random.nextInt(int) and switch-case you can get your solution done.
J Calabrese
Greenhorn

Joined: Jun 10, 2008
Posts: 7
I type casted Math.random() to make it int. Anyways, I got it to work using the switch statement. If anyone else needs help in the future, this is an example they can use:

Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Hi ??? poopshakes ???

Could you take a look at this link.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

"poopshakes"

Please check your private messages.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
J Calabrese
Greenhorn

Joined: Jun 10, 2008
Posts: 7
sorry guys, I changed it. I overlooked that when I was changing my profile and password.
 
GeeCON Prague 2014
 
subject: generating random comments