How do I random out my already asked question (so that it doesn't repeat) in a quiz? What's the best possible way to achieve this without crashing the app?
Problem: I wanted to keep only one Java class with all the questions, don't know if that's a good approach when progressing towards other Level activities. So I tried it with 10 questions to start with, but I keep getting already asked questions. Then I break that class into two (5 questions each) defining each by new level (Easy Level 1 and 2) but the problem is same.
I guess, this is the line I'm struggling with the logic part:
updateQuestion(rand.nextInt(mQuestionsLength));
This is what I tried, till now:
public class Quiz extends AppCompatActivity {
Button answer1, answer2;
TextView score, question, timer;
private Question mQuestions = new Question();
private String mExplanation;
private String mAnswer;
private int mScore = 0;
private int mQuestionNumber = 0;
private String mQuestionsIndex;
private int mAns = 0;
public int mQuestionsLength = mQuestions.mQuestions.length;
Random rand;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_easy_level1);
rand = new Random();
answer1 = (Button)findViewById(R.id.answer1);
answer2 = (Button)findViewById(R.id.answer2);
score = (TextView) findViewById(R.id.score);
question = (TextView) findViewById(R.id.question);
timer = (TextView) findViewById(R.id.timer);
score.setText("Overall Score: " + mScore);
updateQuestion(rand.nextInt(mQuestionsLength));
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(answer1.getText() == mAnswer){
bingoAlert();
} else {
oopsAlert();
}
}
});
answer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(answer2.getText() == mAnswer){
bingoAlert();
} else {
oopsAlert();
}
}
});
}
private void updateQuestion(int num){
question.setText(mQuestions.getQuestion(num));
answer1.setText(mQuestions.getChoice1(num));
answer2.setText(mQuestions.getChoice2(num));
mAnswer = mQuestions.getCorrectAnswer(num);
mQuestionsIndex = mQuestions.getQuestionIndex(num);
mQuestionNumber++;
}
private void bingoAlert(){
mScore+=5;
score.setText("Score: " + mScore);
if(mQuestionNumber == mQuestions.mQuestions.length){
// Do something...
}else{
updateQuestion(rand.nextInt(mQuestionsLength));
}
}
private void oopsAlert(){
mScore-=2;
score.setText("Score: " + mScore);
if(mQuestionNumber == mQuestions.mQuestions.length){
// Do something...
}else{
updateQuestion(rand.nextInt(mQuestionsLength));
}
}
Question Class
public class Questions {
public static String[] mQuestions = new String[]{
"Question 1",
"Question 2",
"Question 3",
"Question 4",
"Question 5"
};
public static String[] mQuestionsIndex = new String[]{
"1", "2", "3", "4", "5"
};
private static String mChoices[][] = {
{"Choice 1", "Choice 2"},
{"Choice 1", "Choice 2"},
{"Choice 1", "Choice 2"},
{"Choice 1", "Choice 2"},
{"Choice 1", "Choice 2"}
};
private String mCorrectAnswers[] =
{"Choice 1", "Choice 2", "Choice 1", "Choice 2", "Choice 1"};
public String getQuestion(int a) {
String question = mQuestions[a];
return question;
}
public String getChoice1(int a) {
String choice = mChoices[a][0];
return choice;
}
public String getChoice2(int a) {
String choice = mChoices[a][1];
return choice;
}
public String getCorrectAnswer(int a) {
String answer = mCorrectAnswers[a];
return answer;
}
public String getQuestionIndex(int a) {
String questionsIndex = mQuestionsIndex[a];
return questionsIndex;
}
This is my Random Class
class RandomClass {
private Random randNum;
public RandomClass() {
randNum = new Random();
}
public int[] generateRandomArray(int arraySize){
int[] theArray = new int[arraySize];
for(int mQuestionNumber = 0; mQuestionNumber <= 5; mQuestionNumber++){
theArray[mQuestionNumber] = randNum.nextInt(6);
}
return theArray;
}
}
And this is my recent attempt:
private void updateQuestion(int num){
question.setText(mQuestions.getQuestion(num));
answer1.setText(mQuestions.getChoice1(num));
answer2.setText(mQuestions.getChoice2(num));
mAnswer = mQuestions.getCorrectAnswer(num);
mExplanation = mQuestions.getExplanation(num);
mQuestionsIndex = mQuestions.getQuestionIndex(num);
for (int mQuestionNumber = 0; mQuestionNumber <= 5; mQuestionNumber++) {
int[] ints = new RandomClass().generateRandomArray(5);
}
}
Thanks!
Tip: Maintain an ArrayList
Again generate a random number within the range of size of the list and repeat the process.
list.get(random_number); will give you the index of the number stored in that position so that you can display your position accordingly.
Let me know if any point is not clear or ambiguous. I'll update it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With