如何仅显示确切的问题数

时间:2013-08-04 14:18:56

标签: c# vb.net

Question set中我收到特定(topicID,Marks)的所有问题。我正在为totqsn显示问题随机,即要显示的问题总数说10个问题,我正在存储特定标记(1,2,3,4)的问题数量这个int变量分别为mark1Qsn,mark2Qsn,mark3Qsn,mark4Qsn, 使用下面的代码,我能够显示来自QuestionSet的问题(比如包含34个带有(TopicID,标记)的问题)totqsn(比如从QuestionSet中随机显示10个问题)。我的问题是 如何显示总共10个问题,其中1个标记的3个问题,2个标记的3个问题,3个标记的1个问题,4个标记的3个问题,即 totqsn(10 questions= 3 qsn_of_mark1 + 3 qsn_of_mark2 + 1 qsn_of_mark3 + 3 qsn_of_mark3)

public partial class GroupExmStart : Form
{        
    DBHandling db = new DBHandling();

    string GrpID = "";
    string TopiID = "";
    int totQsn = 0;
    int mark1qsn = 0;
    int mark2Qsn = 0;
    int mark3Qsn = 0;
    int mark4Qsn = 0;
    int tik = 0;
    string QuestionSet = "";
    static Random _r = new Random();
    string[] randomQsn = null;
    string[] QAndA = null;
    public GroupExmStart(string GroupName, string DurationID)
    {
        InitializeComponent();

        totQsn = Convert.ToInt16(conf[0]);    
        mark1qsn = Convert.ToInt16(conf[3]);//this variable contains number of question to be display of mark 1
        mark2Qsn = Convert.ToInt16(conf[4]);
        mark3Qsn = Convert.ToInt16(conf[5]);
        mark4Qsn = Convert.ToInt16(conf[6]); 

        QuestionSet = db.GetQuestions(TopiID, "1");
        QuestionSet = QuestionSet + db.GetQuestions(TopiID, "2");
        QuestionSet = QuestionSet + db.GetQuestions(TopiID, "3");
        QuestionSet = QuestionSet + db.GetQuestions(TopiID, "4");
        int z = Quiz(QuestionSet);

        foreach (string qa in QAndA.OrderBy(i => _random.Next()))
        {
            if (qa != null)
                if (qa.IndexOf('~') != -1)
                {
                    randomQsn[count] = qa;
                    count++;
                    if (count == totQsn)
                        break;
                }
        }

        int Quiz(string data)
        {
            string[] words = data.Split('$');
            randomQsn = new string[totQsn + 1];
            QAndA = new string[words.Length + 1];
            for (int i = 0; i < words.Length; i++)
            {
                QAndA[i] = words[i];
            }

            return 0;
        }
    }
}

从DBHandling类访问的GetQuestions方法

public string GetQuestions(string TopicID, string Marks)
{
    string data = "";
    try
    {
        string sql = "select QID,Question,Opt1,Opt2,Opt3,Opt4,AnsOp,Marks from Questions where TopicID IN(" + TopicID + ") and Marks=" + Marks;
        cmd = new OleDbCommand(sql, acccon);
        rs = cmd.ExecuteReader();
        while (rs.Read())
        {
            data = data + rs[0].ToString() + "~" + rs[1].ToString() + "~" + rs[2].ToString() + "~" + rs[3].ToString() + "~" + rs[4].ToString() + "~" + rs[5].ToString() + "~" + rs[6].ToString() + "~" + rs[7].ToString() + "$";
        }
    }
    catch (Exception err)
    {
        MessageBox.Show(err.Message.ToString());
    }
    return data;
}

提前感谢您提供任何帮助

1 个答案:

答案 0 :(得分:1)

完全未经测试且非常混乱,但......

 public class Question
    {
        public string Id { get; set; }
        public string Text { get; set; }

        public string Option1 { get; set; }
        public string Option2 { get; set; }
        public string Option3 { get; set; }
        public string Option4 { get; set; }

        public string AnswerOption { get; set; }
        public int Marks { get; set; }
    }

    public IEnumerable<Question> GetQuestions(string topicId, int marks)
    {
        string sql = "select QID,Question,Opt1,Opt2,Opt3,Opt4,AnsOp,Marks from Questions where TopicID IN(" +
                     topicId + ") and Marks=" + marks.ToString();
        var cmd = new OleDbCommand(sql, new OleDbConnection(""));
        var rs = cmd.ExecuteReader();

        if (rs != null)
        {
            while (rs.Read())
            {
                yield return
                    new Question
                        {
                            Id = rs[0].ToString(),
                            Text = rs[1].ToString(),
                            Option1 = rs[2].ToString(),
                            Option2 = rs[3].ToString(),
                            Option3 = rs[4].ToString(),
                            Option4 = rs[5].ToString(),
                            AnswerOption = rs[6].ToString(),
                            Marks = marks
                        };
            }
        }
    }

    public void Foo()
    {
        var totQsn = Convert.ToInt16(conf[0]); // isn't this just the sum of everything else?
        var mark1qsn = Convert.ToInt16(conf[3]); //this variable contains number of question to be display of mark 1
        var mark2qsn = Convert.ToInt16(conf[4]);
        var mark3Qsn = Convert.ToInt16(conf[5]);
        var mark4Qsn = Convert.ToInt16(conf[6]);

        var mark1questionSet = GetQuestions(topicId, 1).ToList();
        var mark2questionSet = GetQuestions(topicId, 2).ToList();
        // etc

        var finalQuestions = new List<Question>();

        for (int i = 0; i < mark1qsn; i++)
        {
            var setIndex = _random.Next(mark1questionSet.Count);
            finalQuestions.Add(mark1questionSet[setIndex]);
            mark1questionSet.RemoveAt(setIndex);
        }

        for (int i = 0; i < mark2qsn; i++)
        {
            var setIndex = _random.Next(mark2questionSet.Count);
            finalQuestions.Add(mark2questionSet[setIndex]);
            mark2questionSet.RemoveAt(setIndex);
        }
        // etc - put this into a method or something
    }