Question

I'm creating a simple quiz game. In this game, the question and the respective answers will be randomly generated. I created a list "q" to the questions.

And for the answers, I created various lists with 4 strings each one. For example, if the question is the 0 in the q list, the answers for this questions will be in the list "a0", right? But I'm having some problem to get the strings in the list of answers. I've tried this:

    while(true){

            Integer nxt = rng.nextInt(6);

            if (!generated.contains(nxt))
            {
                generated.add(nxt);

                textView1.setText(((ArrayList<String>) q).get(nxt));

                String x;
                x = ("a" +nxt); 

                Collections.shuffle((x));

                btn1.setText(((ArrayList<String>) x).get(0));
                btn2.setText(((ArrayList<String>) x).get(1));
                btn3.setText(((ArrayList<String>) x).get(2));
                btn4.setText(((ArrayList<String>) x).get(3));


                break;
            }
    }

I created a string "x" to get the right list. If the "nxt" is 4, the buttons texts will get the strings in the list a4.

But in my code, the "Collections.shuffle" and the "setText" try to find the list "x". It's not going in the way I imagined.

How can I fix it?

*My idea is check the string of the button clicked and compare with another list of RIGHT answers. In that way, I can attributte the right answer and the other 3 wrong.

Was it helpful?

Solution 2

I would be surprised if this code would compile and/or run correctly at all. You are trying to use the content of a String as an variable name, cast that variable to an ArrayList<String> and then to access the elements. This is wrong on so many levels that you should consider doing a few Java tutorials again.

If you do or feel that you can continue anyways, try this approach: You shouldn't store the questions and answers in a separate list, but together in a class.

class Question
{
    //...
    // maybe id and other stuff belonging to a question
    //...

    String questionText;

    // separate because you need to tell the correct answer apart from the wrong ones later
    // you could also just always use the first one in a set of answers.
    String correctAnswerText; 
    ArrayList<String> wrongAnswerTexts;
 }

Then you can store your questions in an ArrayList<Question> in your app and set an answer as follows:

//...
// set up ArrayList<Question> questions here
//...

int nxt = rng.nextInt(6);

//...    
// make sure your list is actually long enough for the generated index
//...

Question nextQuestion = questions.get(nxt);

//...    
// make sure the retrieved object is valid
//...    
// set the question text to nextQuestion.questionText;
//...

ArrayList<String> allAnswers = new ArrayList<String>();
allAnswers.add(nextQuestion.correctAnswerText);
allAnswers.addAll(nextQuestion.wrongAnswerTexts);

Collections.shuffle(allAnswers);

btn1.setText(allAnswers.get(0));
btn2.setText(allAnswers.get(1));
btn3.setText(allAnswers.get(2));
btn4.setText(allAnswers.get(3));

OTHER TIPS

I made a similar quiz app (King of Math) a few days ago.

  1. Calculate the correct answers
  2. Add the correct answer to your answers-list
  3. Calculate fake answers, add them to the answers-list
  4. Shuffle the list
  5. Get the id of the correct answer. It is in the range [0, max_answers)

If an answer has been selected, you check if the selected id (0, 1, 2, 3) is the one of the correct answer. If it is, the user picked the right one, otherwise he didn't.

PS: sorry for the self-promotion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top