如何在gridview中计算多项选择题的分数 [英] How to calculate Score of multiple choice question in a gridview

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

问题描述

使用以下代码计算包含多项选择题的问题纸的得分。但面对的问题是,当论文的所有问题都得到解答时,分数是正确的。假如如果一个人不回答任何问题,那么它显示错误的分数。希望为每个正确的答案更新分数。

  protected   void  RadioButton_CheckedChanged(  object  sender,EventArgs e)
{
string selans = - 1;
int 得分= 0 ;
foreach (GridViewRow行 GridView1.Rows)
{
RadioButton r1 =((RadioButton)row.FindControl( rad1));
RadioButton r2 =((RadioButton)row.FindControl( rad2));
RadioButton r3 =((RadioButton)row.FindControl( rad3));
RadioButton r4 =((RadioButton)row.FindControl( rad4));
HiddenField hdn =((HiddenField)row.FindControl( hf));
if (r1.Checked)
{
selans = 1;
}
其他 if (r2.Checked)
{
selans = 2;
}
其他 如果(r3.Checked)
{
selans = 3;
}
其他 如果(r4.Checked)
{
selans = 4;
}
if (selans == hdn.Value)
{
Score = Score + 1 ;
com = new SqlCommand( 更新考试设置分数=' +分数+ '其中PCode =' + Label14。 Text + ',con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
}
}

解决方案

我认为你在这里采取了错误的方法。您的数据可能来自数据源。该数据源可能包含问题,答案选项和正确答案。确定每个问题的分数或整个测试的分数的逻辑属于业务对象,而不是GUI层。我建议你做以下事情:



- 创建一个问题类,看起来像这样(如果需要你可以改变你的答案,但我希望保持示例简单

  public   class 问题
{
public string 问题{ get ; set ;}

public string AnswerOne { get ; set ;}

public string AnswerTwo { get ; set ;}

public string AnswerThree {获取; set ; }

public string AnswerFour {获得; set ; }

public int CorrectAnswer {获得; set ; }

public bool AnswerOneSelected {获得; set ; }

public bool AnswerTwoSelected {获得; set ; }

public bool AnswerThreeSelected {获得; set ; }

public bool AnswerFourSelected {获得; set ; }

public bool IsAnswerCorrect
{
< span class =code-keyword> get

{
bool [] currentAnswers = new bool [] {AnswerOneSelected,AnswerTwoSelected,AnswerThreeSelected,AnswerFourSelected};
return currentAnswers [CorrectAnswer - 1 ];
}
}

// 您可以使用这些属性您的数据源包含每个问题的不同分数
public int ScoreForCorrectAnswer {获得; set ; }

public int 得分
{
< span class =code-keyword> get

{
return IsAnswerCorrect? ScoreForCorrectAnswer: 0 ;
}
}
}



将这些对象的列表绑定到GridView,将每个Answer ... Selected属性绑定到每个Radiobuttons。



接下来,您可以在对象上实现INotifyPropertyChanged以了解何时选择了不同的答案,或者继续使用您已经使用的CheckedChanged事件使用。



您可以通过查询您的对象集合获得测试分数:

  var  score = questions.Where(q = >  q.IsAnswerCorrect).Count(); 



如果您想使用其他评分机制,请改用Score属性,并执行以下操作:

  var  score = questions.Sum(q = >  q.Score); 


Am using the following code for calculation score of question paper containing multiple choice question. But am facing problem that,the score is correct when all the questions of the paper is answered. Suppose if one doesn't ans any of the question then its showing wrong score. Want that score is updated for each correct answer.

protected void RadioButton_CheckedChanged(object sender, EventArgs e)
    {
        string selans = "-1";
        int Score = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
            RadioButton r1 = ((RadioButton)row.FindControl("rad1"));
            RadioButton r2 = ((RadioButton)row.FindControl("rad2"));
            RadioButton r3 = ((RadioButton)row.FindControl("rad3"));
            RadioButton r4 = ((RadioButton)row.FindControl("rad4"));
            HiddenField hdn = ((HiddenField)row.FindControl("hf"));
            if (r1.Checked)
            {
                selans = "1";
            }
            else if (r2.Checked)
            {
                selans = "2";
            }
            else if (r3.Checked)
            {
                selans = "3";
            }
            else if (r4.Checked)
            {
                selans = "4";
            }
            if (selans == hdn.Value)
            {
                Score = Score + 1;
                com = new SqlCommand("update Exam set Score='" + Score + "' where PCode='" + Label14.Text + "'", con);
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
            }
        }
    }

解决方案

I think you're following the wrong approach here. Your data is probably coming from a datasource. That datasource probably contains the question, the answers options, and the correct answer. The logic to determine the score per question, or the score for the entire test, belongs in your business object, not in your GUI layer. I would advice you to do the following:

- Create a Question class, looking something like this (you can change your answers to be a collection if needed, but I wanted to keep the example simple

public class Question
{
  public string Question { get; set; }

  public string AnswerOne { get; set; }

  public string AnswerTwo { get; set; }

  public string AnswerThree { get; set; }

  public string AnswerFour { get; set; }

  public int CorrectAnswer { get; set; }

  public bool AnswerOneSelected { get; set; }

  public bool AnswerTwoSelected { get; set; }

  public bool AnswerThreeSelected { get; set; }

  public bool AnswerFourSelected { get; set; }

  public bool IsAnswerCorrect
  {
    get
    {
      bool[] currentAnswers = new bool[] { AnswerOneSelected, AnswerTwoSelected, AnswerThreeSelected, AnswerFourSelected };
      return currentAnswers[CorrectAnswer - 1];
    }
  }

  // You can use these properties if your datasource contains different scores per question
  public int ScoreForCorrectAnswer { get; set; }

  public int Score
  {
    get
    {
      return IsAnswerCorrect ? ScoreForCorrectAnswer : 0;
    }
  }
}


Bind a list of these objects to your GridView, binding each Answer...Selected property to each of the Radiobuttons.

Next, you can either implement INotifyPropertyChanged on your objects to know when a different answer is selected, or continue to use the CheckedChanged event that you are already using.

You can get the score for the test by querying your collection of objects like this:

var score = questions.Where(q => q.IsAnswerCorrect).Count();


If you want to use a different scoring mechanism, use the Score property instead, and do something like:

var score = questions.Sum(q => q.Score);


这篇关于如何在gridview中计算多项选择题的分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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