# generating random comments

J Calabrese

Greenhorn

Posts: 7

posted 7 years ago

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 ]

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

J Calabrese

Greenhorn

Posts: 7

posted 7 years ago

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?

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?

posted 7 years ago

Your hint is clear to get your required solution.

I personally suggest you to use

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

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