如何计算C#中在线考试的分数? [英] How do I calculate marks of online exam in C#?

查看:93
本文介绍了如何计算C#中在线考试的分数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在按钮的onclick事件中,我从aspx页面获取控件,并且我还使用了if语句来查看哪个单选按钮处于活动状态并且已在varialbe selans中存储相应的值,使用这个selans,我会将它与隐藏字段的值进行比较,以便查找是否检查单选按钮是否正确答案,如果答案是正确的,即selans中的值与隐藏字段中的值匹配(实际答案)并且变量count(最初用值0初始化)相应地递增,并且所有这些代码都放在for循环中,这将执行直到no。 GridView中的控件(您可以将其与问题编号联系起来,因为每个记录GridView都会生成新的控件)。



我使用了变量



1)totalgrid1: - 从gridview1获得总分



2)totalgrid2: - 获得总分gridview2



3)总计: - totalgrid1 + totalgrid2 ...这是考试总分



4)correct1: - 从gridview1获取正确答案的数量



5)correct2: - 从gridview2获取正确答案的数量



6)正确: - correct1 + correct2 .....用户正确答案总数



运行此项程序即尝试正确的答案我得到得分= 6,而不是在结果页面上得到4,因为只有4个问题,每个问题带有1个标记,那我怎么能得到6分?而且我得到正确答案的数量=没有显示(它是空白的)...它应该显示一些价值。



我找不到在我犯错的地方。下面是我的代码。看看我的代码。告诉我我在哪里犯错误和解决方案是什么



我尝试过:



.aspx.cs: -



On onclick event of the button , I’ve fetch the controls form the aspx page, and further I’ve used if statement in order to see which radio button is active and have store the corresponding value in the varialbe selans, using this selans, I will compare it with the value of hidden field in order to find whether checked radio button is the correct answer or not, if the answer is correct, i.e value in selans matches with the value in hidden field ( the actual answer) and the variable "count" ( initially initialized with value 0) increments accordingly, and all this code is placed in the "for loop" which will execute till the no. of controls in the GridView (you can relate it with the no. of question, as for every record GridView generates new control).

I have used variables

1) totalgrid1:- to get total score from gridview1

2)totalgrid2 :- to get total score from gridview2

3)total :- totalgrid1 + totalgrid2...it is the total score of the exam

4)correct1 :- get the number of correct answers from gridview1

5)correct2 :- get the number of correct answers from gridview2

6)correct :- correct1 + correct2.....total number of correct answers by the user

after running this program i.e attempting the exam with correct answers I am getting "score = 6" instead of getting 4 on result page because there is only 4 questions and each question carry 1 marks then how can I get 6 marks ? and also I am getting "Number of right answers = nothing is shown(it is blank)" ...it should show some value.

I can't find out where I am making mistake. Below is my code .Have a look at my code . Show me where I am making mistake and what is the solution

What I have tried:

.aspx.cs :-

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);

}


}





结果。 aspx.cs: -





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"];
}
}

推荐答案

我认为现在是时候停止猜测你的代码在做什么了。是时候看到你的代码正在执行并确保它能达到预期的效果。



调试器是你的朋友。它会告诉你你的代码到底在做什么。

一步一步地执行,检查变量,你会发现它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



使用调试器检查一切是否符合您的期望。

检查 GridView2 大小是否符合您的期望。
I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Use the debugger to check that everything match your expectations.
Check that GridView2 size match your expectations.


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);

}


}


这篇关于如何计算C#中在线考试的分数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆