I am writing a java program to ask the user 5 random questions from 10 questions stored in an array, output if the answer is correct or incorrect and not to ask the same question more than once. Everything works except for the only asking the question only once. I can't figure out how to do this, can someone please help me?

public static void main(String[] args) //start of main
{
Quiz bank = new Quiz(); //creates new Quiz object
bank.bankList(); //assigns name of portion of program to build the collection of questions and answers
bank.askQuestion(); //assigns name of portion of program to ask the questions
} //end main

I'd shuffle it instead of using Random. Problem with Random is you will need to keep asking it to give you something new and checking to see if its already given it to you.
To shuffle, you'd use collections:

Otherwise, there are a few approaches. You can clone the existing array, and as you pull from it set it to null. Simply loop until you find something. Another is to create an array of int that tracks the position of one pulled. If it matches the random selection, try again.
Random is wrong here though, that should probably be .nextInt(quizBank.length).

shatgid

04-01-2012, 05:55 AM

I appreciate your help, but I'm sorry to say your pretty much talking over my head. I don't really even follow your code. I am new to java and really only know the stuff in my code (barely). I am in an intro-programing class and we flew through java in 5 weeks, so I don't know much more than what you see. My textbook is no help, the teacher isn't following it at all, and I've been searching the net for something similar with no luck.

shatgid

04-03-2012, 01:38 AM

I'm not quite sure how I did it but I got it to work with your code and some searching on the net. I don't fully understand it, but it works. Thank you!!
Below is my code, in case anyone else needs something similar.

public static void main(String[] args) //start of main
{
Quiz bank = new Quiz(); //creates new Quiz object
bank.bankList(); //assigns name of portion of program to build the collection of questions and answers
bank.askQuestion(); //assigns name of portion of program to ask the questions
} //end main