【问题标题】:generate random array String[] without repetition in java (android)在java(android)中生成不重复的随机数组String []
【发布时间】:2014-01-06 09:38:56
【问题描述】:

我正在创建安卓测验应用程序。我已经随机化了它的问题,但有时会重复生成的问题。

如何防止其重复并在所有问题显示后终止其方法?

这是整个工作代码:

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

【问题讨论】:

    标签: java android string repeat


    【解决方案1】:
    1. 给每个问题一个数字 (int) 并假设您有 10 个问题。
    2. 生成 1 到 10 之间的随机数。
    3. 创建一个集合以跟踪您的问题(最好列出..)
    4. 每次生成随机数时,请检查集合是否已包含该数字。 如果是,则生成另一个随机数,否则将其添加到集合中..

    【讨论】:

      【解决方案2】:

      在使用之前使用 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;
            }
        }
      

      http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

      它具有理想的属性,即在初始化后(即 O(N)),数组访问时间是恒定的。拒绝重新绘制元素的算法往往会变慢,因此无法很好地扩展。

      【讨论】:

      • 可以在android中使用吗?
      • 我不明白为什么不这样做。它只使用for 循环和几个分配。你已经在使用Random
      • 仍在分析它,我无法将它应用到我当前的代码中。
      • 该算法自 1930 年代就已经存在,因此您应该没有问题。你可以随时参考我给你的维基百科链接。
      • 嗨,我也试过这个代码和其他代码,但我不能让它工作。我在这里真的很绝望。你能重新编程吗?我知道我问了很多.. :(
      【解决方案3】:

      例如,您可以使用不包含重复项的集合 (HashSet),并使用它来代替 String[]。如果你想要 String[] 数组,你可以很容易地从 HashSet 的内容中创建它

      【讨论】:

      • 有关于 HashSet 的示例代码吗?我对此一无所知,也没有使用过。
      • 你用过ArrayList吗?
      • 从这个角度来看,使用 HashSet 并没有什么不同,因为它们实现了相同的接口
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 2014-04-25
      • 2011-12-28
      • 1970-01-01
      • 2016-03-01
      • 2018-09-14
      相关资源
      最近更新 更多