简体   繁体   English

生成随机数组String [],而无需在Java中重复(android)

[英]generate random array String[] without repetition in java (android)

I'm creating android quiz app. 我正在创建android测验应用程序。 I have randomized its question but sometimes the generated questions are repeated. 我已将其问题随机化,但有时会重复生成的问题。

How to prevent its repetition and terminate its method after all the questions are displayed? 显示所有问题后,如何防止其重复并终止其方法?

Here's the whole working code: 这是整个工作代码:

public class MainActivity extends Activity {

private int currentQuestion;
private String [] questions;
private String [] answers;
private Button answerButton;
private Button questionButton;
private TextView questionView;
private TextView answerView;
private EditText answerText; 

Random random = new Random();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
}

public void init() {
 questions = new String[]{"Q1?","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10"};
 currentQuestion = random.nextInt(questions.length);
 answers = new String[]{"A1","A2","A3","A4","A5","A6","A7","A8","A9","A10"};
 answerButton = (Button)findViewById(R.id.AnswerButton);
 questionButton = (Button)findViewById(R.id.QuestionButton);
 questionView = (TextView) findViewById(R.id.QuestionTextView);
 answerView = (TextView) findViewById(R.id.AnswerTextView);
 answerText = (EditText) findViewById(R.id.AnswerText);

 answerButton.setOnClickListener(new OnClickListener(){
 @Override
 public void onClick(View v) {checkAnswer();
}});

 questionButton.setOnClickListener(new OnClickListener(){
     @Override
 public void onClick(View v) {
 showQuestion();

     }});
}

public int showQuestion(){
currentQuestion++;
if(currentQuestion == questions.length);
 currentQuestion = 0;
currentQuestion = (random.nextInt(questions.length));

questionView.setText(questions[currentQuestion]);
 answerView.setText("");
 answerText.setText("");
    return currentQuestion;
    }

public void checkAnswer() {
String answer = answerText.getText().toString();
 if(isCorrect(answer))
 answerView.setText("You're right!");
 else answerView.setText("Sorry, the correct answer is "+answers[currentQuestion]);
}
}
  1. Give each question a number (int) and assume you have 10 questions. 给每个问题一个数字(int),并假设您有10个问题。
  2. generate random numbers between 1 and 10. 产生1到10之间的随机数。
  3. Create a collection to keep track of your questions (list preferably..) 创建一个集合来跟踪您的问题(最好列出。)
  4. Each time you generate a random number check if the collection already contains the number. 每次生成随机数时,请检查集合中是否已包含该数字。 If yes, generate another random number, else add it to the collection.. 如果是,则生成另一个随机数,否则将其添加到集合中。

Use a Fisher–Yates shuffle to shuffle questions and answers in advance of using them. 使用Fisher-Yates随机播放可在使用之前随机播放questionsanswers

 static void fyShuffle(String[] questions, String[] answers)
 {
     Random rnd = new Random();
     for (int i = questions.length - 1; i > 0; --i){
         int index = rnd.nextInt(i + 1);
         // Swap questions
         String s = questions[index];
         questions[index] = questions[i];
         questions[i] = s;
         // Swap answers
         s = answers[index];
         answers[index] = answers[i];
         answers[i] = s;
      }
  }

See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle 参见http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

It has the desirable property that after initialisation (which is O(N)), array access time is constant. 它具有理想的属性,即在初始化之后(即O(N)),数组访问时间是恒定的。 Algorithms that reject re-drawn elements tend to slow down and so don't scale well. 拒绝重绘元素的算法会减慢速度,因此无法很好地扩展。

you can use a collection that does not hold duplicates ( HashSet ) for instance, and use it instead of a String[] . 例如,您可以使用一个不包含重复项的集合( HashSet ),并使用它代替String[] If you want the String[] array you can easily create it from the HashSet's content 如果您想要String []数组,则可以从HashSet的内容轻松创建它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM