您好 我在asp.net c#中创建在线测验。为此我有一个表格在dropdownlist&中显示testlist。开始按钮。点击第二个表格后,第二个表格显示一个问题标签,答案的radiobuttonlist,next&复选框以供审核。我在1stform的开始按钮单击事件中创建随机问题ID的数组。当我点击第二个表格中的下一个按钮然后出现下一个随机问题时,我想要检查一系列问题以供审查。我使用代码作为值的数组(例如,10101)1用于true& 0表示错误如下,但我想要检查那些问题的数组:
int[] a = (int[])Session["values"];//this is array of random question ids created in 1st form
int g;
if (chkmark.Checked == true)
{
g = 1;
}
else
{
g = 0;
}
int[] chkarray = new int[Convert.ToInt32(Session["Counter"]) - 1];
int[] temp1 = (int[])Session["arrofchk"];
int k, no;
if (temp1 == null)
no = 0;
else
no = temp.Length;
for (k = 0; k < no; k++)
{
chkarray[k] = temp1[k];
}
chkarray[j] = g;
答案 0 :(得分:0)
就个人而言,我会使用Dictionary<int, bool>
。
在字典的key
中,您可以存储随机问题ID,在该对的value
中,您可以存储选中的项目状态。你现在可能需要更多的工作来重构它,但我相信当你想对你的测验项目做更多的动作时,它会为你节省很多时间。
使用字典 - 或者至少是一个精心挑选的集合,我认为更容易获得正确的数据。
对于您的示例,如果两个数组的位置相同,则它只能 。
Dictionary<int, bool> quizAnswers = new Dictionary<int, bool>(); // <questionID, checked>
// Fill dictionary with questions and answers
for(int i=0;i<a.length;i++)
{
if(temp1.length > i) // Make sure we don't get an IndexOutOfBoundsException
{
quizAnswers.Add(a[i], temp1[i] == 1);
}
}
// Get Answered question in array ( LINQ )
int[] checkedAnswers = (from KeyValuePair<int, bool> pair in quizAnswers
where pair.Value == true
select pair.Key).ToArray<int>();
我在这里使用词典的原因是因为我个人认为它比两个单独的数组更整洁。
我相信你应该在测验中实现一个字典,而不是那些数组。如果数组索引不匹配,或者您想要将问题动态添加到固定大小的数组等,该怎么办。
这是需要考虑的事情。希望我能帮到你。