我在弄一个在线答题模块,现在的问题是:选了答案后 在答题卡里面无法显示考生选择的答案。
在答题界面里通过下面的代码实现记录考生答案 //将学生选择的答案记录到studentAnswer[]中
private void radioBtA_Click(object sender, EventArgs e)
{
QuizHelper.studentAnswers[QuesIndex] = Convert.ToString(((RadioButton)sender).Tag);
}在答题卡界面通过下面的代码实现显示考生的答案 private void AnswerCard_Load(object sender, EventArgs e)
{
countdown.Start();//启动倒计时
int index = 0;
foreach (Control item in this.Controls)
{
if (item is Label)//如果是标签
{
index = Convert.ToInt32(item.Tag);
if (index != -1)
{
item.Text = QuizHelper.studentAnswers[index];
}
}
}
}但是,运行后答题卡里面不显示考生答案
在答题界面里通过下面的代码实现记录考生答案 //将学生选择的答案记录到studentAnswer[]中
private void radioBtA_Click(object sender, EventArgs e)
{
QuizHelper.studentAnswers[QuesIndex] = Convert.ToString(((RadioButton)sender).Tag);
}在答题卡界面通过下面的代码实现显示考生的答案 private void AnswerCard_Load(object sender, EventArgs e)
{
countdown.Start();//启动倒计时
int index = 0;
foreach (Control item in this.Controls)
{
if (item is Label)//如果是标签
{
index = Convert.ToInt32(item.Tag);
if (index != -1)
{
item.Text = QuizHelper.studentAnswers[index];
}
}
}
}但是,运行后答题卡里面不显示考生答案
studentAnswers 里有些什么内容,跟 index = Convert.ToInt32(item.Tag) 的 index 能不能对上
再在item.Text = QuizHelper.studentAnswers[index] 这行设置断点,然后继续逐语句运行,
到index = Convert.ToInt32(item.Tag);这里时 Tag 是19,index 也是19,
item.Text = QuizHelper.studentAnswers[index]里面 index 是19,studentAnswers[0]对应的是我选的A,这句执行过后再看 item.Text 里面却是空的,也就是并没有将studentAnswers[0]里面的A放到item.Text里面去。
这个要怎么解决呢!
radioBtA_Click 里只给了 QuizHelper.studentAnswers[0] 赋值,其他 index 的都空着,AnswerCard_Load 里只有等到 foreach 循环到 index 为 0 时,再看看 item.Text = QuizHelper.studentAnswers[index] 之后 item.Text 有没有值
第一行说错了
QuesIndex 和 Label.Tag 是不是对应的?
QuesIndex是0,item.Tag 是19
但是现在交卷出问题了,
private void Result_Load(object sender, EventArgs e)
{
//答对的题目数
int correctNum = 0;
for (int i = 0; i < QuizHelper.questionNum; i++)
{
if (QuizHelper.studentAnswers[i] == QuizHelper.correctAnswers[i])
{
correctNum++;
}
}
提示说索引超出范围,这是肿么回事呢?
我是在答题界面 读取题目信息的时候将标准答案放进List<string> correctAnswers里面的,不知道 这里是否有问题
public void GetQuestion()
{
lblQuesId.Text = "题 " + (QuesIndex + 1).ToString();
int quesId = QuizHelper.selectedQuestionId[QuesIndex];
string sql = "select Question,OptionA,OptionB,OptionC,OptionD,Answer from Question where QuestionId=" + quesId + "";
try
{
DBHelper.conn.Open();
SqlCommand cmd = new SqlCommand(sql, DBHelper.conn);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
riTBQuestion.Text = sdr["Question"].ToString();
radioBtA.Text ="A."+sdr["OptionA"].ToString();
radioBtB.Text ="B."+ sdr["OptionB"].ToString();
radioBtC.Text ="C."+sdr["OptionC"].ToString();
radioBtD.Text ="D."+sdr["OptionD"].ToString();
QuizHelper.correctAnswers.Add(sdr["Answer"].ToString());
}
sdr.Close();
}
你这答题系统是不是一次一题,答好一题才显示下一题?
定义了QuizHelper这个类,里面定义了这些东东:
public static List<int> allQuestionIds=new List<int>();//所有题目 Id
public static bool[] selectedStates=new bool[50];//题目是否已被抽中
public static int questionNum=20;//题目数量
public static int[] selectedQuestionId=new int[20];//选中题目的Id
public static List<string> correctAnswers = new List<string>();//标准答案
public static string[] studentAnswers=new string[20];//学员用户的答案
“你这答题系统是不是一次一题,答好一题才显示下一题?”是的
我设断点调试了一下,发现List<string> correctAnswers 里面只有点击查看了的题的答案,也就是说不能将List<string> correctAnswers放在读取题目那里获取答案,但是要怎么改呢
for (int i = 0; i < QuizHelper.studentAnswers.Count; i++)
{
if (QuizHelper.studentAnswers[i] == QuizHelper.correctAnswers[i])
{
correctNum++;
}
}