Java Android 测验应用程序 - 使问题随机和动态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19011875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:20:38  来源:igfitidea点击:

Android Quiz App - Make Questions Random and Dynamic

javaandroidsqlitemobile

提问by Kezlyr

I am working on building a quiz app for Android. Once completed, the app with have around 500 questions, existing in multiple categories.

我正在为 Android 构建一个测验应用程序。完成后,该应用程序有大约 500 个问题,存在于多个类别中。

I can get the questions to work fine, but only in one instance: the questions are linear, meaning that they always appear in the same order and the question always has the same answers in the same order (so, for example, Question 1 always has Answers A, B, and C, Question 2 always has Answers D, E, F, etc.) I know the reason for this since I'm just swapping textviews, basically, but only because I don't know how to do what I'm trying to do.

我可以让问题正常工作,但仅限于一种情况:问题是线性的,这意味着它们总是以相同的顺序出现,并且问题总是以相同的顺序有相同的答案(例如,问题 1 总是有答案 A、B 和 C,问题 2 总是有答案 D、E、F 等)我知道这样做的原因,因为我基本上只是在交换文本视图,但只是因为我不知道该怎么做我正在尝试做的事情。

  1. I want the questions within each set to go in a random order without duplicates. In a previous project, to accomplish something similar I created an integer that would generate a random value and then check to see if that value has been used already. However, that would be incredibly memory-inefficient and since I'm working on an Android app and not a PC one, that's too important to overlook.
  2. I want each question to have random answers, other than the correct one, chosen from a pool of possible answers. So, for example, if I were to ask something like "Who ruled Britain from 1810-1820" (no actual idea on the answer), I'd want one answer to be the correct one and the other two or three to be taken from a pool of 10 European rulers. Additionally, I want the placement of the correct answer to be dynamic, so it isn't always in the same spot (so people don't just memorize the question and it's answer's location).
  1. 我希望每组中的问题按随机顺序排列,没有重复。在以前的项目中,为了完成类似的事情,我创建了一个整数,它会生成一个随机值,然后检查该值是否已被使用。然而,这将是令人难以置信的内存效率低下,而且由于我正在开发 Android 应用程序而不是 PC 应用程序,这太重要了,不容忽视。
  2. 我希望每个问题都有随机的答案,而不是正确的答案,从可能的答案池中选择。因此,例如,如果我问诸如“谁在 1810 年至 1820 年间统治英国”之类的问题(对答案没有实际想法),我希望一个答案是正确的,而其他两个或三个答案是正确的来自 10 位欧洲统治者。此外,我希望正确答案的位置是动态的,因此它并不总是在同一个位置(因此人们不只是记住问题及其答案的位置)。

As of now, the questions are just in a java file since I'm still in the pre-alpha stage, but I plan on moving it to a SQLite database once I get the framework for the project figured out.

到目前为止,这些问题只是在一个 java 文件中,因为我仍处于 pre-alpha 阶段,但我计划在弄清楚项目的框架后将其移动到 SQLite 数据库中。

采纳答案by Saif Hamed

My suggestion is you should randomize the order of the quiz's array with an integer array that contain numbers from 0 to 499. all questions will shown but not in same order. to do this, see this post: Random shuffling of an array

我的建议是,您应该使用包含 0 到 499 数字的整数数组随机化测验数组的顺序。所有问题都将显示,但顺序不同。要做到这一点,请参阅这篇文章:数组的随机洗牌

the second case, if I'm in your place, I will make an array of the answers, put random answers, and take a random number from 0 to 3 for the index of the right answer and

第二种情况,如果我在你的位置上,我会制作一个答案数组,放置随机答案,并取一个从 0 到 3 的随机数作为正确答案的索引和

answer[random]=correctAnswer;

Regards

问候

回答by Kaushik Sivakumar

for 1)

1)

Write down the questions from 1 to N. Pick a random number k between one and the number of unstruck numbers remaining (inclusive). Counting from the low end, strike out the kth question not yet struck out, and write it down elsewhere. Repeat from step 2 until all the numbers have been struck out. The sequence of questions written down in step 3 is now a random permutation of the original numbers.

写下从 1 到 N 的问题。在 1 和剩余的未命中数(含)之间选择一个随机数 k。从低端数起,把还没打出来的第k题打出来,写在别处。从第 2 步开始重复,直到所有数字都被删除。步骤 3 中写下的问题序列现在是原始数字的随机排列。

this is nothing but.. Fischer Yates Shuffle which you could look at http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

这不过是 .. Fischer Yates Shuffle 你可以看看 http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

回答by imranhasanhira

Look at the following code

看下面的代码

//Take a list to store the index of already asked questions
HashSet<Integer> alreadyAskedQuestions = new HashSet<Integer>();

Random random = new Random();

//change the number according to your need
int maxQuestionCount = 500; 

//Continue untill all the questions are completed 
while (alreadyAskedQuestions.size() < maxQuestionCount) {

    // Get a question that is not already asked
    int currentQuestionIndex = random.nextInt(maxQuestionCount);
    while (alreadyAskedQuestions.contains(currentQuestionIndex)) {
        currentQuestionIndex = random.nextInt(maxQuestionCount);
    }

    // do something with current question

    // Put the answers into a list
    ArrayList<String> answers = new ArrayList<String>();
    answers.add("right answer");
    answers.add("wrong answer 1");
    answers.add("wrong answer 2");
    answers.add("wrong answer 3");

    //Shuffle the answers
    Collections.shuffle(answers);

    // do something with the answers

    // put the current question in the already asked list
    alreadyAskedQuestions.add(currentQuestionIndex);
}

回答by cafebabe1991

If i understood your question right,you actually need an efficient way to randomize the index at which questions are placed ....

如果我正确理解您的问题,您实际上需要一种有效的方法来随机化放置问题的索引......

Do this it'll give you random number efficiently

这样做它会有效地给你随机数

Use this number as an index to your stored questions and show them

使用此数字作为您存储问题的索引并显示它们

int t=(int)System.currentTimeMillis();
int index=t%500;

Access your question

Questions[index]

This will give a question always in a random order..

这将始终以随机顺序给出一个问题。

Hope it helps you.

希望对你有帮助。