重复数字随机 Android Studio Java 随机数组问题

huangapple go评论78阅读模式
英文:

Repeating numbers random Android studio java random array problem

问题

我在数组中使用随机数遇到了问题。这有点像一个问答游戏,我有一些包含问题、选项和答案的数组。当我开始我的问答游戏时,问题会重复出现。

我了解到有关洗牌(shuffle)的内容,但不知道如何使用它。

我有一个方法:

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);
}

它通过一个数字从另一个类中的数组更新我的问题。这个数字我是用随机数生成的:

updateQuestion(r.nextInt(mQuestionslength));

更新问题的示例:

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);
    }
});

我需要以一种不会重复的方式来处理随机数 r。我应该如何做呢?非常感谢!

英文:

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!

答案1

得分: 1

首先,预先创建一个随机问题列表,用于展示问题。

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);
}
// rlist 的输出类似于:[2, 3, 4, 1] 或者 [1, 3, 4, 2]

然后,在点击按钮后对索引进行递增,并生成当前编号对应的问题。

int index = 0;
updateQuestion(rlist.get(index));
answer1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (index < 4) {
            if(answer1.getText() == mAnswear){
                mScore++;
                mScorecurrent.setText("Рахунок: " + mScore);
            }
    
            updateQuestion(rlist.get(index));
            index++;
        } else {
            // 在最后一个问题之后要执行的操作
        }
    }
});
英文:

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

Random rand = new Random();
List&lt;Integer&gt; rlist = new ArrayList&lt;&gt;();
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 &lt; 4) {
            if(answer1.getText()==mAnswear){
                mScore++;
                mScorecurrent.setText(&quot;Рахунок: &quot;+mScore);
            }

            
            updateQuestion(rlist.get(index));
            index++;
            } else {
                //To do after the last question
            }
        }
    });

答案2

得分: 1

你可以创建一个整数ArrayList来存储已经显示给用户的问题编号,下次在设置问题之前,你可以检查问题编号是否已经在ArrayList中。如果没有,在向用户提问。

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

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&lt;int&gt; num  = ArrayList&lt;int&gt;()
    int temp = r.nextInt(mQuestionslength)
    while(num.contains(temp))
    {
        temp = r.nextInt(mQuestionslength)
    }
    num.add(temp);
    updateQuestion(temp);

答案3

得分: 0

有一个著名的算法叫做Fisher-Yates洗牌算法。这里是Java的一个实现。您可以使用它来对答案数组进行洗牌。

英文:

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.

huangapple
  • 本文由 发表于 2020年6月5日 23:57:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/62219441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定