I am developing an application which involves a quiz in it. I have used a viewflipper for displaying question, as i press the next button it flips to next question. However, I wish to display these questions randomly. I have parsed an xml and displayed the questions n its options !
Assuming that all your questions stored in array or some data structure e.g. ArrayList
You need to define a single random object only once in your activity
e.g.
Random random = new Random();
Get the next question index:
int nextQuestionIndex = random.nextInt(n); // where n is the total number of questions
Related
I am having a problem with the Quiz part of my Application. I need a 100 question per category, but I just need to display 20 question to be answer by the user. That 100 question used to randomize the questions to have different questions to display. How do i limit the question?
You can create an int. Every time you open a random question you set your int + 1. And if your int is 20, you stop opening a new question.
I'm not sure exactly what you're asking, but:
Let's assume you have a Java/Android "Quiz" app, and you want to randomly select and display 20 questions from a list of 100.
Let's also assume that your 100 questions are stored in a Java "List<>".
Finally, let's assume you have a "Question" class that contains both the question and the corresponding answer.
Then one possible solution might be:
// Fetch 20 random questions
List<Question> selectedQuestions = new ArrayList<Question>();
Random random = new Random();
for (int i=0; i < 20; i++) {
int idx = random.nextInt(questionsList.size());
selectedQuestions.add( questionsList.get(idx));
}
// Display these questions
return selectedQuestions;
I'm making a quiz app I have string array and I want to load them on button click, strings should be loaded randomly into Text View. Strings should not be repeated.
Thanks..
Because it's your quiz, I'm gonna give you hints not the whole soltion
Random rnd = new Random;
while(array.size()>0){
// 1. use rng to get a index between 0 to current array.size()
// 2. remove the string by array.remove(index) so that it won't duplicated.
// 3. setText(string)
}
Try this, you can add comments if you need more details.
One way to do it is to convert the array to a list and shuffling it.
List myList = Arrays.asList(yourArray);
Collections.shuffle(myList);
After that you can just iterate over the list.
I am currently creating an app that fetches 2 Strings randomly from an arraylist.
This is what I did:
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("someString");
// continue arrayList.add() for another thousand times for different strings.
// I am adding the Strings manually as these are specific questions in a quiz app
Random random = new Random();
myTextView.setText(arrayList.get(random.nextInt(arrayList.size());
myTextView2.setText(arrayList.get(random.nextInt(arrayList.size());
The Question:
At any point in time, I only need 2 Strings to be displayed to the user. But I am populating a thousand element arrayList, and fetching only 2 Strings from there. Is there a better way to do this? For example, only populating 2 Strings at runtime, without populating the unnecessary Strings?
Or maybe it does not matter at all?
Thank you very much in advance!
I have an array of strings that I am displaying in my activity using a random number every time a button called Next is clicked. How do I create another button called Back, that simply displays the previously or last generated strings in the order they were randomly generated?. So that instead of clicking next to generate another string, I can click Back at any time to view all the previous strings one by one and then click Next again to continue the random selection.
Create a array list of String like
ArrayList<String> arraylist= new ArrayList<String>();
after that using add function add the strings that you are displaying in your activity using a random number every time a button called Next is clicked.
arraylist.add("your_randomly_generated_string");
now your randomly generated string will stored in arraylist one by one
This will store the string now how to retrieve?
following steps may help you not sure ...
While adding the randomly generated string into arraylist initialize the int variable with zero and increment its value after each adding...
public int indexvalue=0;
on your Onclick of NEXT button add two lines
arraylist.add("your_randomly_generated_string");
indexvalue++;
And now on the Onclick of BACK Button Use indexvalue to retrieve...
String data = arraylist.get(indexvalue);
indexvalue--;
here Decremented the indexvalue to retrieve the previous string..
hope you'll understand what im trying to say little bit of lengthy explanation ...
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.
I made a similar quiz app (King of Math) a few days ago.
Calculate the correct answers
Add the correct answer to your answers-list
Calculate fake answers, add them to the answers-list
Shuffle the list
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.
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));