我如何在 C# 中计算在线考试的分数?
How do I calculate marks of Online exam in c#?
在按钮的 onclick 事件上,我从 aspx 页面获取控件,并且进一步使用 if 语句来查看哪个单选按钮处于活动状态并将相应的值存储在 varialbe selans
,使用这个 selans
,我会将它与隐藏字段的值进行比较,以确定选中的单选按钮是否是正确答案,如果答案正确,即 [=12 中的值=] 与隐藏字段中的值(实际答案)匹配,变量“count”(初始值为 0)相应地递增,所有这些代码都放在“for 循环”中,该循环将执行到第 1 个。 GridView 中的控件数量(您可以将其与问题编号相关联,因为对于每条记录,GridView 都会生成新控件)。
我用过变量
1) totalgrid1
:- 从 gridview1
获取总分
2)totalgrid2
:- 从 gridview2 获取总分
3)total
:- totalgrid1
+ totalgrid2
...就是考试的总分
4)correct1
:- 从 gridview1 获取正确答案的数量
5)correct2
:- 从 gridview2 中获取正确答案的数量
6)correct
:- correct1
+ correct2
.....用户的正确答案总数
在 运行 这个程序之后,即尝试正确答案的考试,我得到 "score = 6" 而不是在结果页面上得到 4,因为只有 4 个问题,每个问题有 1 分,那么怎么能我得6分?和
我也得到 "Number of right answers = nothing is shown(it is blank)" ...它应该显示一些价值。
我找不到我哪里出错了。下面是我的代码。看看我的代码。告诉我哪里出错了,解决办法是什么
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Student_Examdemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions");
GridView1.DataBind();
GridView2.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions WHERE SectionId=2");
GridView2.DataBind();
}
}
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
protected void btn_Click(object sender, EventArgs e)
{
RadioButton r1, r2, r3, r4;
HiddenField hdn;
int count = 0;
int neg = 0;
int total=0;
int totalgrid1=0;
int totalgrid2=0;
int attempt1 = 0;
int attempt2 = 0;
int Tattempt = 0;
int correct = 0;
int correct1 = 0;
int correct2 = 0;
string selans = "-1";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
r1 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad1");
r2 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad2");
r3 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad3");
r4 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad4");
hdn = (HiddenField)GridView1.Rows[i].Cells[0].FindControl("hf");
if (r1.Checked)
{
selans = r1.Text;
}
else if (r2.Checked)
{
selans = r2.Text;
}
else if (r3.Checked)
{
selans = r3.Text;
}
else if (r4.Checked)
{
selans = r4.Text;
}
if(r1.Checked || r2.Checked || r3.Checked || r4.Checked)
{
attempt1++;
if (hdn.Value == selans)
{
count++;
correct1++;
}
else
{
neg--;
}
}
totalgrid1 = count + neg;
}
for (int i = 0; i < GridView2.Rows.Count; i++)
{
r1 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad1");
r2 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad2");
r3 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad3");
r4 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad4");
hdn = (HiddenField)GridView2.Rows[i].Cells[0].FindControl("hf");
if (r1.Checked)
{
selans = r1.Text;
}
else if (r2.Checked)
{
selans = r2.Text;
}
else if (r3.Checked)
{
selans = r3.Text;
}
else if (r4.Checked)
{
selans = r4.Text;
}
if (r1.Checked || r2.Checked || r3.Checked || r4.Checked)
{
attempt2++;
if (hdn.Value == selans)
{
count++;
correct2++;
}
else
{
neg--;
}
}
totalgrid2 = count + neg;
}
total = totalgrid1 + totalgrid2;
Tattempt = attempt1 + attempt2;
correct = correct1 + correct2;
Label2.Text = total.ToString();
Label3.Text = Tattempt.ToString();
Label4.Text = correct.ToString();
Response.Redirect("/Student/Result.aspx?Score=" + Label2.Text +"&AttemptedQues=" +Label3.Text+ "&CorrectAns" +Label4.Text);
}
}
Result.aspx.cs :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Student_Result : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.QueryString["Score"];
Label2.Text = Request.QueryString["AttemptedQues"];
Label3.Text = Request.QueryString["CorrectAns"];
}
}
我通过在按钮的 onclick 事件上放置断点来知道我在哪里犯了错误。
解决方法如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Student_Examdemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions");
GridView1.DataBind();
GridView2.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions WHERE SectionId=2");
GridView2.DataBind();
}
}
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
protected void btn_Click(object sender, EventArgs e)
{
RadioButton r1, r2, r3, r4;
HiddenField hdn;
int count1 = 0;
int count2 = 0;
int neg1 = 0;
int neg2 = 0;
int total=0;
int totalgrid1=0;
int totalgrid2=0;
int attempt1 = 0;
int attempt2 = 0;
int Tattempt = 0;
int correct = 0;
int correct1 = 0;
int correct2 = 0;
string selans = "-1";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
r1 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad1");
r2 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad2");
r3 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad3");
r4 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad4");
hdn = (HiddenField)GridView1.Rows[i].Cells[0].FindControl("hf");
if (r1.Checked)
{
selans = r1.Text;
}
else if (r2.Checked)
{
selans = r2.Text;
}
else if (r3.Checked)
{
selans = r3.Text;
}
else if (r4.Checked)
{
selans = r4.Text;
}
if(r1.Checked || r2.Checked || r3.Checked || r4.Checked)
{
attempt1++;
if (hdn.Value == selans)
{
count1++;
correct1++;
}
else
{
neg1--;
}
}
totalgrid1 = count1 + neg1;
}
for (int i = 0; i < GridView2.Rows.Count; i++)
{
r1 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad1");
r2 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad2");
r3 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad3");
r4 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad4");
hdn = (HiddenField)GridView2.Rows[i].Cells[0].FindControl("hf");
if (r1.Checked)
{
selans = r1.Text;
}
else if (r2.Checked)
{
selans = r2.Text;
}
else if (r3.Checked)
{
selans = r3.Text;
}
else if (r4.Checked)
{
selans = r4.Text;
}
if (r1.Checked || r2.Checked || r3.Checked || r4.Checked)
{
attempt2++;
if (hdn.Value == selans)
{
count2++;
correct2++;
}
else
{
neg2--;
}
}
totalgrid2 = count2 + neg2;
}
total = totalgrid1 + totalgrid2;
Tattempt = attempt1 + attempt2;
correct = correct1 + correct2;
Label2.Text = total.ToString();
Label3.Text = Tattempt.ToString();
Label4.Text = correct.ToString();
Response.Redirect("/Student/Result.aspx?Score=" + Label2.Text +"&AttemptedQues=" +Label3.Text+ "&CorrectAns=" +Label4.Text);
}
}
在按钮的 onclick 事件上,我从 aspx 页面获取控件,并且进一步使用 if 语句来查看哪个单选按钮处于活动状态并将相应的值存储在 varialbe selans
,使用这个 selans
,我会将它与隐藏字段的值进行比较,以确定选中的单选按钮是否是正确答案,如果答案正确,即 [=12 中的值=] 与隐藏字段中的值(实际答案)匹配,变量“count”(初始值为 0)相应地递增,所有这些代码都放在“for 循环”中,该循环将执行到第 1 个。 GridView 中的控件数量(您可以将其与问题编号相关联,因为对于每条记录,GridView 都会生成新控件)。
我用过变量
1) totalgrid1
:- 从 gridview1
2)totalgrid2
:- 从 gridview2 获取总分
3)total
:- totalgrid1
+ totalgrid2
...就是考试的总分
4)correct1
:- 从 gridview1 获取正确答案的数量
5)correct2
:- 从 gridview2 中获取正确答案的数量
6)correct
:- correct1
+ correct2
.....用户的正确答案总数
在 运行 这个程序之后,即尝试正确答案的考试,我得到 "score = 6" 而不是在结果页面上得到 4,因为只有 4 个问题,每个问题有 1 分,那么怎么能我得6分?和 我也得到 "Number of right answers = nothing is shown(it is blank)" ...它应该显示一些价值。
我找不到我哪里出错了。下面是我的代码。看看我的代码。告诉我哪里出错了,解决办法是什么
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Student_Examdemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions");
GridView1.DataBind();
GridView2.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions WHERE SectionId=2");
GridView2.DataBind();
}
}
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
protected void btn_Click(object sender, EventArgs e)
{
RadioButton r1, r2, r3, r4;
HiddenField hdn;
int count = 0;
int neg = 0;
int total=0;
int totalgrid1=0;
int totalgrid2=0;
int attempt1 = 0;
int attempt2 = 0;
int Tattempt = 0;
int correct = 0;
int correct1 = 0;
int correct2 = 0;
string selans = "-1";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
r1 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad1");
r2 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad2");
r3 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad3");
r4 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad4");
hdn = (HiddenField)GridView1.Rows[i].Cells[0].FindControl("hf");
if (r1.Checked)
{
selans = r1.Text;
}
else if (r2.Checked)
{
selans = r2.Text;
}
else if (r3.Checked)
{
selans = r3.Text;
}
else if (r4.Checked)
{
selans = r4.Text;
}
if(r1.Checked || r2.Checked || r3.Checked || r4.Checked)
{
attempt1++;
if (hdn.Value == selans)
{
count++;
correct1++;
}
else
{
neg--;
}
}
totalgrid1 = count + neg;
}
for (int i = 0; i < GridView2.Rows.Count; i++)
{
r1 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad1");
r2 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad2");
r3 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad3");
r4 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad4");
hdn = (HiddenField)GridView2.Rows[i].Cells[0].FindControl("hf");
if (r1.Checked)
{
selans = r1.Text;
}
else if (r2.Checked)
{
selans = r2.Text;
}
else if (r3.Checked)
{
selans = r3.Text;
}
else if (r4.Checked)
{
selans = r4.Text;
}
if (r1.Checked || r2.Checked || r3.Checked || r4.Checked)
{
attempt2++;
if (hdn.Value == selans)
{
count++;
correct2++;
}
else
{
neg--;
}
}
totalgrid2 = count + neg;
}
total = totalgrid1 + totalgrid2;
Tattempt = attempt1 + attempt2;
correct = correct1 + correct2;
Label2.Text = total.ToString();
Label3.Text = Tattempt.ToString();
Label4.Text = correct.ToString();
Response.Redirect("/Student/Result.aspx?Score=" + Label2.Text +"&AttemptedQues=" +Label3.Text+ "&CorrectAns" +Label4.Text);
}
}
Result.aspx.cs :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Student_Result : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.QueryString["Score"];
Label2.Text = Request.QueryString["AttemptedQues"];
Label3.Text = Request.QueryString["CorrectAns"];
}
}
我通过在按钮的 onclick 事件上放置断点来知道我在哪里犯了错误。
解决方法如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Student_Examdemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions");
GridView1.DataBind();
GridView2.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions WHERE SectionId=2");
GridView2.DataBind();
}
}
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
protected void btn_Click(object sender, EventArgs e)
{
RadioButton r1, r2, r3, r4;
HiddenField hdn;
int count1 = 0;
int count2 = 0;
int neg1 = 0;
int neg2 = 0;
int total=0;
int totalgrid1=0;
int totalgrid2=0;
int attempt1 = 0;
int attempt2 = 0;
int Tattempt = 0;
int correct = 0;
int correct1 = 0;
int correct2 = 0;
string selans = "-1";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
r1 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad1");
r2 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad2");
r3 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad3");
r4 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad4");
hdn = (HiddenField)GridView1.Rows[i].Cells[0].FindControl("hf");
if (r1.Checked)
{
selans = r1.Text;
}
else if (r2.Checked)
{
selans = r2.Text;
}
else if (r3.Checked)
{
selans = r3.Text;
}
else if (r4.Checked)
{
selans = r4.Text;
}
if(r1.Checked || r2.Checked || r3.Checked || r4.Checked)
{
attempt1++;
if (hdn.Value == selans)
{
count1++;
correct1++;
}
else
{
neg1--;
}
}
totalgrid1 = count1 + neg1;
}
for (int i = 0; i < GridView2.Rows.Count; i++)
{
r1 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad1");
r2 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad2");
r3 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad3");
r4 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad4");
hdn = (HiddenField)GridView2.Rows[i].Cells[0].FindControl("hf");
if (r1.Checked)
{
selans = r1.Text;
}
else if (r2.Checked)
{
selans = r2.Text;
}
else if (r3.Checked)
{
selans = r3.Text;
}
else if (r4.Checked)
{
selans = r4.Text;
}
if (r1.Checked || r2.Checked || r3.Checked || r4.Checked)
{
attempt2++;
if (hdn.Value == selans)
{
count2++;
correct2++;
}
else
{
neg2--;
}
}
totalgrid2 = count2 + neg2;
}
total = totalgrid1 + totalgrid2;
Tattempt = attempt1 + attempt2;
correct = correct1 + correct2;
Label2.Text = total.ToString();
Label3.Text = Tattempt.ToString();
Label4.Text = correct.ToString();
Response.Redirect("/Student/Result.aspx?Score=" + Label2.Text +"&AttemptedQues=" +Label3.Text+ "&CorrectAns=" +Label4.Text);
}
}