0

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.

  • First of all, `x` is not a list, it's a `String`. Perhaps you meant `q`; that's used as a list earlier? – Rob I Mar 04 '13 at 15:34
  • random answers for questions? or randomly selected questions, which each have their own set of non-random answers? if they're random answers, how can you be sure any of them actually do answer the question? – Marc B Mar 04 '13 at 15:34
  • Reminds me of https://play.google.com/store/apps/details?id=com.troubi.kingofmath – poitroae Mar 04 '13 at 15:34
  • The answers will be displayed in a random way too. That's why I put the Collections.shuffle before get the texts to the button. –  Mar 04 '13 at 15:40
  • My idea is generate something like that: 1 - generate a random number "nxt"; 2 - get the QUESTION with this number "nxt"; 3 - manipulate the name of the list of answers that will be "accessed". If the question is the number 3 in the "q" list, the list of answers accessed will be the "a +nxt" , in that case, nxt = 3 –  Mar 04 '13 at 15:44

2 Answers2

1

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.

Community
  • 1
  • 1
poitroae
  • 21,129
  • 10
  • 63
  • 81
  • 1
    Ok. It's seems a fun app. My idea is similar to yours, but I'm having problem to access the list of answers. How can I manipulate the NAME of the list that will be accessed? That's the problem I can't fix –  Mar 04 '13 at 15:47
  • @SirRoyce You cannot manipulate variable names at runtime. That is at least what I understood. Please explain in further detail what you mean. – poitroae Mar 04 '13 at 15:48
0

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));
Thrakbad
  • 2,728
  • 3
  • 23
  • 31
  • Thank you for the tips, man! I'm new in Java programming and I'm really have no time to follow the tutorials step by step, but I'll try to do this. I used your tips in the app and, after seeing some tutorials and understanding some concepts, now the app is working fine. So, thanks. –  Mar 06 '13 at 04:26