【问题标题】:Quiz questions duplicate issue测验问题重复问题
【发布时间】:2018-07-01 13:17:34
【问题描述】:

我是 Android Java 编程的新手,我的测验问题(总共 50 个问题)重复出现时遇到了问题。我的问题和答案已添加到地图中。然后我调用我的方法 setQuestion() 来显示我的问题和答案,它们都运行良好。

Map<String, String> userQuestions = new HashMap<String, String>();

int questionNumber = 1;

//all my questions go here

setQuestion();
}

public void setQuestion() 
{

Random randomQuestions = new Random();             
    List questions = new ArrayList<>();                          
    List repeatedQuestions = new ArrayList<>();         
    for (int i = 0; i <questionNumber; i++) 
       {
        while (true) 
          {
            questionNumber = randomQuestions.nextInt(50) +1;
            if (!questions.contains(randomQuestions)) 
              {                                                                                        
                repeatedQuestions.add(repeatedQuestions);
                break;
              }
           }
        }    
} 

我的第二次尝试:

List<String> list = new ArrayList<String>(userQuestions.keySet()); 
Collections.shuffle(list, new Random(questionNumber)); 

我尝试将我的 HashMap 转换为列表,希望能够使用 Collections.shuffle。不幸的是,这并没有改组,我的问题也不是随机的。

我的第三次尝试:

Random randomQuestions = new Random();
List<Integer> questions = new ArrayList<Integer>();

Integer questionNumber = randomQuestions.nextInt(50) + 1;
    if (!questions.contains(questionNumber))
    {
       questions.add(questionNumber);
    }                                                                                   

这确实使我的问题随机化,但我仍然得到重复。我真的很困惑为什么它没有添加我以前调用的问题编号?

【问题讨论】:

  • 也许你的while语句有问题?如果问题是重复的,大多数原因是 while 语句。
  • 当你做 questions.contains(randomquestions) 这真的没有按照你的想法做。你真的应该使用嵌套的 for 循环。但我也很难遵循您的代码,因为它的语法不正确
  • 如果您有一个列表,假设有 1,000 个问题,然后您想随机选择 50 个问题。你会做一个基于 50 的 for 循环。使用 Random() 获取 1-1000 之间的随机数。一旦你有了这个, list.get(randomNumber) 得到那个问题。这是最简单的方法,如果您需要查看代码,请告诉我。
  • 我为我的语法道歉,如果你能告诉我代码,我们将不胜感激。谢谢。我有 50 个问题,我想看看,然后测验结束。
  • 请帮助,我尝试了许多不同的方法,但我无法防止重复。哈希映射是否无法防止重复条目?。

标签: java android for-loop while-loop repeat


【解决方案1】:

在Hashmap中存储问题不是一个好主意,您可以尝试使用Suitcase(Structure)的ArrayList来存储小数据和Database用于大数据。

现在可以在每次使用 Shuffle 方法时生成随机问题。

示例

 public void shuffle_ques() {

     // arrQuestions to store to all Questions and Answers
     // arrQuestions.size() gives total number of questions in arrQuestions

    for (int i = 0; i < arrQuestions.size(); i++) 
    { 
        this.arrshuffle.add(i); // arrshuffle to store all the values of i
    }

    shuffle(arrshuffle); // shuffling arrshuffle so we get rearranged values of i in array

}

现在我们可以使用重新排列的 i 值作为加载问题的索引。

LoadQues 方法

 public void LoadQues() {

    // int count = 0; (Global)
    // when calling LoadQues() method set count++ for next random ques.
    /* setting ques on textview by getting ques from arrQuestions
       and getting any random index number for question from arrshuffle.*/

    txtques.setText(arrQuestions.get(arrshuf.get(count)).Ques);
    btn1.setText(arrQuestions.get(arrshuf.get(count)).Opt1);
    btn2.setText(arrQuestions.get(arrshuf.get(count)).Opt2);
    btn3.setText(arrQuestions.get(arrshuf.get(count)).Opt3);
    btn4.setText(arrQuestions.get(arrshuf.get(count)).Opt4);
    ans = arrQuestions.get(arrshuf.get(count)).Ans;


}

此代码将在应用程序中以及在您重新启动应用程序时加载随机问题。

【讨论】:

  • 非常感谢您编写代码并解释它。我同意你的方法是更有效的方法。我只是想知道,当我的测验快完成时,为什么我的 For 循环无法工作?我很困惑为什么它没有将以前提出的问题添加到我的数组中进行检查。再次感谢。
猜你喜欢
  • 2019-11-07
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
相关资源
最近更新 更多