I'm creating a java GUI based program, and I have an array of questions. I need to create a method that gets the next question after I click a JButton
. The next question should be chosen randomly and shouldn't repeat a previously-asked question. See the code below -- how would getNextQuestion
be implemented?
public class QuestionBank {
String [] questions;
int currQuestionIndex;
public QuestionBank() {
questions = new String [10]; //increase array size if you will add more questions
questions[0]= "Which three words describe you best?";
questions[1]= "Which is your best feature?";
questions[2]= "Which common saying or phrase describes you?";
questions[3]= "What’s the best thing that’s happened to you this week?";
questions[4]= "Who was your role model when you were a child?";
questions[5]= "Who was your favorite teacher and why?";
questions[6]= "What was your favorite subject at school?";
questions[7]= "What did you want to be when you grew up?";
questions[8]= "If you could have one wish come true what would it be?";
questions[9]= "Which would you prefer — three wishes over five years or one wish right now?";
//add more questions
}
public String getNextQuestion() {
//get the next question, randomly from the array and without repeats
}
}