我是 C# 的新手,试图制作一个本质上是一个包含 30 个问题的调查的程序,这些问题通过选择五个单选按钮(非常不同意、不同意...非常同意等)中的一个来回答。
我已经设置了一个小“块”代码,它将检查哪个单选按钮检查了问题并将一个值分配给一个数组(见下文)。
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonScore_Click(object sender, EventArgs e)
{
this.textBoxScoreOutput.Text = " ";
int[] score = new int[2]; // Declares the integer of score and sets it to a value of zero
// Question 1
if (radioButtonSD1.Checked == true) // If Strongly Disagree checked give score a value of 1
score[0] = 1;
else if (radioButtonD1.Checked == true) // If Disagree checked give score a value of 2
score[0] = 2;
else if (radioButtonNS1.Checked == true) // If Not Sure checked give score a value of 3
score[0] = 3;
else if (radioButtonA1.Checked == true) // If Agree checked give score a value of 4
score[0] = 4;
else if (radioButtonSA1.Checked == true) // If Strongly Agree is checked give score a value of 5
score[0] = 5;
// Question 2
if (radioButtonSD2.Checked == true) // If Strongly Disagree checked give score a value of 1
score[1] = 1;
else if (radioButtonD2.Checked == true) // If Disagree checked give score a value of 2
score[1] = 2;
else if (radioButtonNS2.Checked == true) // If Not Sure checked give score a value of 3
score[1] = 3;
else if (radioButtonA2.Checked == true) // If Agree checked give score a value of 4
score[1] = 4;
else if (radioButtonSA2.Checked == true) // If Strongly Agree is checked give score a value of 5
score[1] = 5;
// Output values in array to text box
this.textBoxScoreOutput.Text = "Array: ";
foreach (int i in score)
{
this.textBoxScoreOutput.Text += "[" + i.ToString() + "] ";
}
int sum = score.Sum();
this.textBoxScoreOutput.Text += "The Sum of the array is: " + sum.ToString();
}
}
}
因此,这是检查 30 个问题中的前两个问题,并且完全按照我的需要和认为的方式工作。
我想知道我是否可以只循环这些“块”中的一个并让它检查所有 30 个问题。我已经搜索和搜索,但找不到我正在寻找的确切内容(我也知道我可能也没有寻找正确的东西)。
我只是想避免在我的程序中有三十个这样的“块”。我觉得其中的 30 个会变得一团糟。这可能吗?