嗨,我想知道是否有任何方法可以简化以下IF语句。我有47个问题要回答,我真的不想一直重复这个问题。我不熟悉 c#,想要帮忙。
protected void GA_Total(object sender, EventArgs e)
{
// Generalized Anxiety Total
int GATotal = 0;
Label1.Text = Convert.ToString(GATotal);
//-----------Question 1----------------
if (RadioButtonList.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 13----------------
if (RadioButtonList1.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList1.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList1.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList1.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 22----------------
if (RadioButtonList2.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList2.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList2.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList2.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 27----------------
if (RadioButtonList3.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList3.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList3.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList3.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 35----------------
if (RadioButtonList4.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList4.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList4.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList4.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 37----------------
if (RadioButtonList5.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList5.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList5.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList5.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//------------Results-------------------
Label1.Text = Convert.ToString(GATotal);
}
您基本上是将项目的索引添加到GATOTAL中,为什么不直接使用RadioButtonList的SelectedIndex呢?
例如
GATotal += RadioButtonList.SelectedIndex +
RadioButtonList1.SelectedIndex +
RadioButtonList2.SelectedIndex +
RadioButtonList3.SelectedIndex +
RadioButtonList4.SelectedIndex +
RadioButtonList5.SelectedIndex;
GATotal += RadioButtonList.SelectedIndex + RadioButtonList1.SelectedIndex ...
如果要
为所选项目分配不同的值,此方法将变得容易:
protected void GA_Total(object sender, EventArgs e)
{
// Generalized Anxiety Total
int GATotal = 0;
GATotal += sumSelectedItems(radioButtonList);
GATotal += sumSelectedItems(radioButtonList1);
GATotal += sumSelectedItems(radioButtonList2);
GATotal += sumSelectedItems(radioButtonList3);
GATotal += sumSelectedItems(radioButtonList4);
GATotal += sumSelectedItems(radioButtonList5);
Label1.Text = GATotal.toString();
}
private int sumSelectedItems(RadioButtonList list)
{
int sum = 0;
for (int i = 0; i < list.Items.Count; i++)
{
if (list.Items[i].Selected)
{
sum += i;
}
}
return sum;
}
假设您有表格中的所有单选按钮
<asp:Table ID="Table1" runat="server"><asp:RadioButtonList runat="server"></asp:RadioButtonList></asp:Table>
下面的代码适用于任意数量的单选按钮。
foreach (Control ctrl in Table1.Controls)
{
if (ctrl is RadioButtonList)
{
RadioButtonList rbl = (RadioButtonList)ctrl;
for (int i = 0; i < rbl.Items.Count; i++)
{
if (rbl.Items[i].Selected)
{
GATotal+= RadioButtonList.SelectedIndex;
}
}
}
}