2 u011100157 u011100157 于 2013.06.17 13:37 提问

基于c#+.net 编写的在线考试页面无法正确加载 及无法正确评分

“求各位大神帮帮忙修改下 下面的代码”
c#+.net做一个课程设计 考试页面实现加载三道题 点击提交后并自动的判断 得出分数我自己做的这个在线考试页面无法正确记录考题数量,加载考题的时候也无法正确加载三道题 i的值总是超出 求各位大神帮帮忙
在线考试页面
public partial class student_startexam : System.Web.UI.Page{
public string rightans = null;
//存储正确答案
public static int tnum;
//记录考题数量
protected void Page_Load(object sender, EventArgs e)
{

lastunum.Text = Session["studentnum"].ToString();
lastuname.Text = Session["stuname"].ToString();
lastusex.Text = Session["stusex"].ToString();
exkm.Text = Session["KM"].ToString();
Label1.Text = "考试时间为10分钟,每小题2分,考试已用时:";
SqlConnection con = BaseClass.DBcon();
con.Open();
int i = 1;
SqlCommand cmd = new SqlCommand("select top 3* from Test where testCourse='"+Session["KM"].ToString()+"' order by newid()",con);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
Literal littxt = new Literal();
Literal litti = new Literal();
RadioButtonList cbk = new RadioButtonList();
cbk.ID = "cbk" + i.ToString();
//显示标题
littxt.Text = i.ToString() + "、"+Server.HtmlEncode(sdr["testContent"].ToString())+ "";
litti.Text = ""; cbk.Items.Add("A."+Server.HtmlEncode(sdr["testAns1"].ToString())); cbk.Items.Add("B." + Server.HtmlEncode(sdr["testAns2"].ToString())); cbk.Items.Add("C." + Server.HtmlEncode(sdr["testAns3"].ToString())); cbk.Items.Add("D." + Server.HtmlEncode(sdr["testAns4"].ToString()));
cbk.Font.Size = 11;
string id = cbk.ID;
for (int j = 1; j <= 4; j++)
{
string value=cbk.Items[j - 1].ToString().Substring(2);
cbk.Items[j - 1].Value = j.ToString();
}
rightans += sdr[5].ToString();//正确答案
Session["rightans"] = rightans;
Panel1.Controls.Add(littxt);
Panel1.Controls.Add(cbk);
Panel1.Controls.Add(litti);
i++;
}
tnum++;
sdr.Close(); con.Close();
}
protected void btnsu_Click(object sender, EventArgs e)
{
SqlConnection con = BaseClass.DBcon();
con.Open();
string msc = "";
for (int i = 1; i <tnum; i++)
// for (int i = 1; i <=1; i++)
{
RadioButtonList list = (RadioButtonList)Panel1.FindControl("cbk"+i.ToString()) as RadioButtonList;

        if (list != null)
        {
            if (list.SelectedValue.ToString() != "")
            {
                //msc += list.SelectedValue.Substring(2,list.SelectedValue.Length-2).ToString();//存储考生答案
               // msc += list.SelectedValue.Trim().ToString();
                //msc += list.Text.Trim();
               // msc += list.SelectedItem.Text.ToString();
                msc += list.SelectedItem.Text.Substring(2, list.SelectedItem.Text.Length - 2).ToString();


            }
            else msc += "0";
        }
    }
    Session["sans"] = msc;

    con.Close();

    Response.Redirect("result.aspx?BInt="+tnum.ToString());
   // Response.Redirect("result.aspx");

}

}

自动评分页面

public partial class student_result : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = BaseClass.DBcon();
con.Open();
string Rans = Session["rightans"].ToString();
int j = Convert.ToInt32(Request.QueryString["BInt"]);
string Sans = Session["sans"].ToString();
//int Sans_index = Sans.IndexOf(",");
int stuscore = 0;
for (int i = 0; i < j;i++ )
// for (int i = 0; i < 1;i++ )
{
if (Rans.Substring(i,1).Equals(Sans.Substring(i, 1)))
// if (Rans.ToString() == Sans.ToString())
//string s = Sans.Substring(i, Sans_index);
//if (1 == Rans.IndexOf(s))
{
stuscore += 2;

                SqlCommand cmd = new SqlCommand("update Score set score='" + stuscore + "' where StudentID='" + Session["studentnum"].ToString() + "'and LessonName='" + Session["KM"].ToString() + "'");
                cmd.Connection = con;
                //cmd.ExecuteNonQuery();

                con.Close();
                //Rans = Rans.Substring(Sans_index + 1);
                //Sans_index = Rans.IndexOf(",");
            }
            else
            {

            }
        }
        this.lblresult.Text = stuscore.ToString();
        this.lblkm.Text = Session["KM"].ToString();
        this.lblnum.Text = Session["studentnum"].ToString();
        this.lblname.Text = Session["stuname"].ToString();


        lbldate.Text = DateTime.Now.ToLongTimeString();
        Label10.Text = DateTime.Now.ToLongDateString();
    }
}

1个回答

Hooh2006
Hooh2006   2018.01.12 15:18

你这么乱的编码谁愿意看?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!