Java- Non repeating random questions.
I'm new in Java Programming and so i've been trying to do new things and get to learn :)
Just wondering if anyone could help me on this one,
I'm trying to do a program that displays several questions when started, however I want them to be displayed in a random order in wich they dont get repeated, then check if the right option for the question has been answered (out of a multiple choice). For example, the program has a database of 10 questions, how do i get 5 of those questions displayed randomly, without repetitions, (I kinda figured I can work the other part of the program with a switch inside a switch for every question to keep record of how many have been answered wrong and how many have been answered right)... I know this might not seem like a big deal, but hey, im new XD. Any help welcomed.
Thanks
Re: Java- Non repeating random questions.
Put the questions into a collection and use a random number to chose one and remove it from the collection.
Re: Java- Non repeating random questions.
Are the questions simply Strings? Use a Set to make sure you don't get duplicates (or you could just add a check to make sure your List doesn't contain that question already).
Or you could populate a List with all of the questions, shuffle the List, then use the first x items in it.
Re: Java- Non repeating random questions.
Quote:
Originally Posted by
KevinWorkman
Are the questions simply Strings? Use a Set to make sure you don't get duplicates (or you could just add a check to make sure your List doesn't contain that question already).
Or you could populate a List with all of the questions, shuffle the List, then use the first x items in it.
Mmm and that would be done how XD? Kinda new, (3 or 4 days into java that is XD)
Re: Java- Non repeating random questions.
Read the API doc or tutorial on how to create a List.
Java Platform SE 6
The Java™ Tutorials
Or search for code examples on this forum.