1

i have a problen with random in arrays. It is like a quiz and i have arrays with questions,choises and answers. When i start my quiz , qustions are repeating Know about shuffle but do not know how to use it i have a method:

 private void updateQuestion(int num){
    question.setText(mQuestionAvtor.getQuestion(num));
    answer1.setText(mQuestionAvtor.getChoice1(num));
    answer2.setText(mQuestionAvtor.getChoice2(num));
    answer3.setText(mQuestionAvtor.getChoice3(num));
    answer4.setText(mQuestionAvtor.getChoice4(num));
    mAnswear= mQuestionAvtor.getCorrectAnswear(num);
}

It updates my questions from another Class with arrays by a num. This num i did by random :

updateQuestion(r.nextInt(mQuestionslength));

Example of updating questions

  answer1 = (Button) findViewById(R.id.answear1);
    answer2 = (Button) findViewById(R.id.answear2);
    answer3 = (Button) findViewById(R.id.answear3);
    answer4 = (Button) findViewById(R.id.answear4);
    question = (TextView) findViewById(R.id.question);


    n = r.nextInt(mQuestionslength);
    while (n == n_before)
        n = r.nextInt(mQuestionslength);
    n_before = n;
    updateQuestion(n);
    answer1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(answer1.getText()==mAnswear){
                mScore++;
                mScorecurrent.setText("Рахунок: "+mScore);
            }

            n = r.nextInt(mQuestionslength);
            while (n == n_before) n = r.nextInt(mQuestionslength);
            n_before = n;
            updateQuestion(n);
        }
    });

I need to do it in a such way , that r will not repeat. How can I do it? Thanks in advance!

3
  • Do you mean you want to prevent showing the same question again? Commented Jun 5, 2020 at 16:04
  • Yes! i have same questions repeating Commented Jun 5, 2020 at 16:04
  • As an aside, answer1.getText()==mAnswear will not work as this is a check for reference to a common object. For String comparison, use mAnswear.equals(answer1.getText()). Commented Jun 5, 2020 at 18:33

3 Answers 3

1

First pre-create a random list of the questions you want to show.

Random rand = new Random();
List<Integer> rlist = new ArrayList<>();
while (rlist.size() != 4) {
int r = rand.nextInt(4) + 1;
if (!rlist.contains(r))
       rlist.add(r);
}
//output of rlist is like: [2, 3, 4, 1] or [1, 3, 4, 2]

Then count an index up after clicking the button and generate get a question with the current number.

int index = 0;
updateQuestion(rlist.get(index));
    answer1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (i < 4) {
            if(answer1.getText()==mAnswear){
                mScore++;
                mScorecurrent.setText("Рахунок: "+mScore);
            }


            updateQuestion(rlist.get(index));
            index++;
            } else {
                //To do after the last question
            }
        }
    });
Sign up to request clarification or add additional context in comments.

14 Comments

Dont know why , just pasted this code but there are anyway repeating questions.
@Jugjin Try to put int n = 0; int n_before = 0; in your class and not in any void or int. (Over your onCreate method)
Sorry for a stupid question. If i have have method updateQuestion(n) 4 times in my code - i should not write your code 4 times , just updateQuestion(n), right? Now questions are not repeating because they are not updating. When i choose correct answer - score is increasing but questions are not changed.
@Jugjin Let's try something. Replace your (4 times) updateQuestion(n) with n = r.nextInt(mQuestionslength); while (n == n_before) n = r.nextInt(mQuestionslength); n_before = n; updateQuestion(n);
Same. Questions are not changing.
|
1

You can create an ArrayList of integers to store questions number already shown to the user and next time before setting question you can check if the questions number was already in ArrayList or not, If not ask the question to user.

    ArrayList<int> num  = ArrayList<int>()
    int temp = r.nextInt(mQuestionslength)
    while(num.contains(temp))
    {
        temp = r.nextInt(mQuestionslength)
    }
    num.add(temp);
    updateQuestion(temp);

Comments

0

There's a famous algorithm called the Fisher-Yates Shuffle. Here is an implementation in Java. You can use it to shuffle the array of answers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.