Skip to content

Java - Need help to get random question from an array for my text based game #145247

Discussion options

You must be logged in to vote

Since you're using the java.util.random library, you'll need to first initialize a Random object. Once you've done that, and assuming you are adding your questions to the questions[] array:

You can use the nextInt method to pass in the length of your questions array, so that it returns a random integer between 0 and the length of your array. You can use this as a random index to pull a question, i.e. questions[randomIndex]

I'm linking the docs here as well for you to read over yourself if you need any further information

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@DumbestPerson224
Comment options

Answer selected by DumbestPerson224
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Programming Help Programming languages, open source, and software development.
4 participants