Java Help

petrides

Beta member
Messages
1
This is a very simple problem but im having serious problems!!!

Im making a simple program for uni in a basic java class and im needing to select a random string from an array.

this is my method code that i need help with..


public static void correct()
{
String[] reply = {"Well Done", "Excellent!", "Your Hot!", "Very good!", "Your On Fire!"};

JOptionPane.showMessageDialog (null, reply);

}

this gives me the whole array as the reply in the diologue box, i need it to give a random string as the reply.

Any help will be greatly appriecated!!
 
I haven't done Java in literally years... So, I'll give you a very general suggestion. It's up to you to make it work.

Use Math.Random to get a random integer between 0-4, then use the JOptionPane..... to show reply[randomInt].
 
As stated above, you need to give the index of which string you want. just putting reply in with most languages would give an error, but seems that java just gives you everything, interesting. What mambug stated would be a great way to do it and should be very simple to implement (1 or 2 lines max).
 
it would look somthing like this,

public static void correct()
{
int randarray = 0;
String[] reply = {"Well Done", "Excellent!", "Your Hot!", "Very good!", "Your On Fire!"};
Math.random() * 6;


JOptionPane.showMessageDialog (null, reply[randarray]);

}

or at least something like that,
 
Back
Top Bottom