• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generating random comments

 
J Calabrese
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ??? poopshakes ???

Could you take a look at this link.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"poopshakes"

Please check your private messages.
 
J Calabrese
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry guys, I changed it. I overlooked that when I was changing my profile and password.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic