本文介绍了我的代码中未包含的RandomNumber语录(来自数据库的语录)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的专家,

Dear Experts,

private int GetRandomNumber(int[] levelIds)
       {
           bool exists = false;
           int randomIndex = 0;
           do
           {
               Random rand = new Random();
               randomIndex = rand.Next(levelIds.Length);
               if (completedQuestionIds.Count > 0)
                   exists = completedQuestionIds.Contains(levelIds[randomIndex]);
           }
           while (exists == true);

           completedQuestionIds.Add(levelIds[randomIndex]);

           Session["CompletedQuestionIds"] = completedQuestionIds;

           return levelIds[randomIndex];
       }


预先感谢您的宝贵答复.


Thanks in advance for your valuable reply

推荐答案

ArrayList availableQuestions = new ArrayList();
ArrayList completedQuestions = new ArrayList();
public Form1()
{
    InitializeComponent();

    for (int i = 1; i <= 10; i++)
    {
        availableQuestions.Add(i);
    }
}
private void button1_Click(object sender, EventArgs e)
{
    GetRandomNumber();
}
private void GetRandomNumber()
{
    if (availableQuestions.Count >= 1)
    {
        Random rand = new Random();
        int QuestionID = Convert.ToInt16(availableQuestions[rand.Next(0, availableQuestions.Count - 1)]);
        completedQuestions.Add(QuestionID);
        availableQuestions.Remove(QuestionID);
    }
}


如果您对此有任何疑问,请告诉我.

谢谢,
Imdadhusen


Please do let me know, if you have any doubt for the same.

Thanks,
Imdadhusen


这篇关于我的代码中未包含的RandomNumber语录(来自数据库的语录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 23:30