C# 带有单选按钮和for循环的If-else语句

C# 带有单选按钮和for循环的If-else语句,c#,undeclared-identifier,C#,Undeclared Identifier,我遇到了一个不断发生的问题,我不确定问题是什么。我需要在Microsoft Visual studio中创建一个程序,其中radioButton1用于查找连续数字的总和,radioButton2用于查找数字的阶乘 将有三个按钮,一个用于循环,一个用于WHILE循环,一个用于DO-WHILE循环。我正在做FOR按钮 我试图做的是通过选择,然后按下其中一个按钮,它将使用在消息框中单击的循环找到答案 到目前为止,我得到的是: private void button1_Click(object send

我遇到了一个不断发生的问题,我不确定问题是什么。我需要在Microsoft Visual studio中创建一个程序,其中radioButton1用于查找连续数字的总和,radioButton2用于查找数字的阶乘

将有三个按钮,一个用于循环,一个用于WHILE循环,一个用于DO-WHILE循环。我正在做FOR按钮

我试图做的是通过选择,然后按下其中一个按钮,它将使用在消息框中单击的循环找到答案

到目前为止,我得到的是:

private void button1_Click(object sender, System.EventArgs e)
{
  if ((radioButton1.Checked == true) && (radioButton2.Checked == false))
  {
    int sum = 0;
    int number = int.Parse(numericUpDown1.Value.ToString());
    for (int i = 1; i <= number; i++)
    {
      sum = sum + i;
      MessageBox.Show("The sum of the consecutive numbers leading up to " + number + " is " + sum + ".");
    }
    MessageBox.Show("The sum of the consecutive numbers leading up to " + number + " is " + sum + ".");
  }
  else if ((radioButton2.Checked == true) && (radioButton1.Checked == false))
  {
    int product = 1;
    int number = int.Parse(numericUpDown1.Value.ToString());
    for (int i = 1; i <= number; i++)
    {
      product *= i;
      MessageBox.Show("The factorial of the numbers leading up to " + number + " is " + product + ".");
    }
    MessageBox.Show("The factorial of the numbers leading up to " + number + " is " + product + ".");
  }
  else
  {
    MessageBox.Show("Invalid");
  }
}
private void按钮1\u单击(对象发送者,System.EventArgs e)
{
如果((radioButton1.Checked==true)和&(radioButton2.Checked==false))
{
整数和=0;
int number=int.Parse(numericUpDown1.Value.ToString());

对于(int i=1;i您可能无意中向该单选按钮添加了一个处理程序(例如,在设计器中双击它),然后将其删除。当解决方案生成时,它正在查找该函数,但找不到它

检查Lab5.Designer.cs中的radioButton2代码。它将如下所示:

 this.radioButton1.AutoSize = true;
 this.radioButton1.Location = new System.Drawing.Point(102, 162);
 this.radioButton1.Name = "radioButton1";
 this.radioButton1.Size = new System.Drawing.Size(85, 17);
 this.radioButton1.TabIndex = 1;
 this.radioButton1.TabStop = true;
 this.radioButton1.Text = "radioButton1";
 this.radioButton1.UseVisualStyleBackColor = true;
 this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged); /* THis is the offending line! */
最后一行尝试添加引用不存在方法的事件处理程序。您可以在此处删除它


此外,当程序生成时,您应该能够双击编译错误,IDE将带您找到问题的根源。

看起来像是一个家庭作业。无论哪种方式,您都需要为事件CheckChanged的单选按钮实现一个处理程序。此外,如果您将这些单选按钮放在一个组中,则可以对其中任何一个进行检查和更改在checkchanged事件中,您应该填充一些functionMode或其他内容。例如,当选中RadioButton1时,您应该存储一个变量,该变量将包含要应用的公式,在另一个选中的变量上,您应该更改该公式。在button submit事件中,使用该变量查看当前模式并应用该公式。

哪一行您上面的代码该错误消息指的是什么?它的基本意思是,在某个地方,您有一个类似于
Lab5的方法调用。CheckedChanged(…)
,但
Lab5
(不管是什么)没有
CheckedChanged
方法。我很确定它是在aspx文件的客户端代码中抛出的。您可以用
RadioButton
标记向我们显示您的HTML代码吗?您应该给出您的控件names@LinusCaldwell:并不是说你错了,但你认为这是ASP.NET在哪里?在我看来像WinForms…”如果(foo==true)”是“if(foo)”的一个较长且可读性较差的等价物,if(foo==false)与if(!foo)相同。这真的帮助很大!在那之后它确实起了作用,所以谢谢!@KJohns Cool。是的,双击设计器很容易意外添加事件处理程序。如果这解决了所有问题,请随意接受它作为答案。