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.

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 ]

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

posted

0

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?

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

posted

0

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: