Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java- Non repeating random questions.

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,106
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default 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.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java- Non repeating random questions.

    Quote Originally Posted by KevinWorkman View Post
    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)

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,106
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default 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.

  6. The Following User Says Thank You to Norm For This Useful Post:

    arkaneraven (June 15th, 2011)

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 04:46 PM
  2. JAVA INTERVIEW QUESTIONS
    By kanchana1 in forum Java Theory & Questions
    Replies: 4
    Last Post: June 8th, 2011, 08:23 PM
  3. Repeating different parts of a program
    By swiftxjames in forum Loops & Control Statements
    Replies: 3
    Last Post: November 17th, 2010, 03:29 PM
  4. Java Questions! :)
    By xs4rdx in forum Java Theory & Questions
    Replies: 0
    Last Post: February 21st, 2010, 07:40 AM
  5. Repeating program issue
    By Bill_H in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2009, 10:58 AM

Tags for this Thread