计算ASP.NET在线考试网站的百分比和最终分数 [英] Calculating percentage and final score of an ASP.NET online examination website

查看:93
本文介绍了计算ASP.NET在线考试网站的百分比和最终分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net工作在线考试网站项目我的堆栈是我想要显示最终得分和考试百分比我已经做了足够的显示这个但它显示最终得分= 0和百分比= 0到现在这里是我的代码请帮助我....谢谢你



我尝试过:



 protected void Page_Load(object sender,EventArgs e)
{
SqlCommand com = new SqlCommand(Select * from [Biology],con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dt2 = new DataTable();
da.Fill(dt2);
grdquestions.DataSource = dt2;
grdquestions.DataBind();


}

protected void examsubmit_Click(object sender,EventArgs e)
{
SqlCommand com = new SqlCommand(Select * from [生物学],con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dt = new DataTable();
da.Fill(dt);

int correctAnswers = 0,incorrectAnswers = 0,
totalMarks = 0,totalQuestions = grdquestions.Rows.Count;

for(int i = 0; i< totalQuestions; i ++)
{
GridViewRow row = grdquestions.Rows [i];
string answer = dt.Rows [i] [Answer]。ToString();
int marks = Convert.ToInt32(dt.Rows [i] [Marks]);
var radioButtons = new List< radiobutton>
{
(RadioButton)row.FindControl(rdOption1),
(RadioButton)row.FindControl(rdOption2),
(RadioButton)row.FindControl(rdOption3 ),
(RadioButton)row.FindControl(rdOption4),
};

foreach(var radioButton in radioButtons)
{
if(radioButton.Checked)
{
if(radioButton.Text == dt.Rows [ i] [答案] .ToString())
{
correctAnswers ++;
totalMarks + = marks;
}
else
{
incorrectAnswers ++;
radioButton.Checked = false;
}
}
else if(radioButton.Text == dt.Rows [i] [Answer]。ToString())
{
correctAnswers ++;
radioButton.Checked = true;

}
}
}

Label2.Text =FinalScore is:+ totalMarks;

string correctAnswerPercentage =(correctAnswers / totalQuestions).ToString(0.00%);
Label2.Text =百分比是+ correctAnswerPercentage;
}
}

解决方案

因为 correctAnswers totalQuestions 是整数, correctAnswers / totalQuestions 也将返回一个整数值。由于 correctAnswers 永远不会超过 totalQuestions 该部门只能返回0或1!



您可以尝试这样做:

 

string correctAnswerPercentage =(correctAnswers * 100 / totalQuestions).ToString(00%);或者这个:

 

string correctAnswerPercentage =((float)correctAnswers * 100.0 /(float)totalQuestions )的ToString( 00.0%);


iam working on online examination website project in asp.net my stack is that i want to display final score and exam percentage i have done enough to display this but its showing final score = 0 and percentage = 0 till now here is my code please help me.... thank u

What I have tried:

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlCommand com = new SqlCommand("Select * from [Biology]", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataTable dt2 = new DataTable();
        da.Fill(dt2);
        grdquestions.DataSource = dt2;
        grdquestions.DataBind();


    }

    protected void examsubmit_Click(object sender, EventArgs e)
    {
        SqlCommand com = new SqlCommand("Select * from [Biology]", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataTable dt = new DataTable();
        da.Fill(dt);

    int correctAnswers = 0, incorrectAnswers = 0,
    totalMarks = 0, totalQuestions = grdquestions.Rows.Count;

  for (int i = 0; i < totalQuestions; i++)
    {
    GridViewRow row = grdquestions.Rows[i];
    string answer = dt.Rows[i]["Answer"].ToString();
    int marks = Convert.ToInt32(dt.Rows[i]["Marks"]);
    var radioButtons = new List<radiobutton>
    {
        (RadioButton)row.FindControl("rdOption1"),
        (RadioButton)row.FindControl("rdOption2"),
        (RadioButton)row.FindControl("rdOption3"),
        (RadioButton)row.FindControl("rdOption4"),
    };

    foreach (var radioButton in radioButtons)
    {
        if (radioButton.Checked)
        {
            if (radioButton.Text == dt.Rows[i]["Answer"].ToString())
            {
                correctAnswers++;
                totalMarks += marks;
            }
            else
            {
                incorrectAnswers++;
                radioButton.Checked = false;
            }
        }
        else if (radioButton.Text == dt.Rows[i]["Answer"].ToString())
        {
            correctAnswers++;
            radioButton.Checked = true;
            
        }
    }
}

Label2.Text = "FinalScore is :" + totalMarks;

string correctAnswerPercentage = (correctAnswers / totalQuestions).ToString("0.00%");
Label2.Text = "Percentage is "+correctAnswerPercentage;
    }
}

解决方案

Because correctAnswers and totalQuestions are integers, correctAnswers / totalQuestions will also return an integer value. And since correctAnswers can never be greater than totalQuestions the division can only ever return 0 or 1!

You could try doing this:

string correctAnswerPercentage = (correctAnswers * 100 / totalQuestions).ToString("00%");Or this:

string correctAnswerPercentage = ((float)correctAnswers * 100.0 / (float) totalQuestions).ToString("00.0%");


这篇关于计算ASP.NET在线考试网站的百分比和最终分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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