【问题标题】:What is the C# way equivalent of this for loop?与此 for 循环等效的 C# 方式是什么?
【发布时间】:2014-01-23 21:46:57
【问题描述】:

我对 C# 很陌生,每天都在学习它的元素,来自 C 也让我感到困惑,因为像这样的简单 for 循环可以工作,那么为什么它不能在 C# 中工作,如果可以的话详细解释(如果可能的话)我将不胜感激。

for (int i = 1; i <= 10; i++){

    public void question()
    {
        if (questionNr == 1)
        {
            questionLabel.Text = "What is Chuck's full name?";
            ans1.Text = "Charles Irving Bartowski";
            ans2.Text = "Charles Richard Bartowski";
            ans3.Text = "Charles Luke Bartowski";
            ans4.Text = "Zachary Strahovski";
        }
        else if (questionNr == 2)
        {
            questionLabel.Text = "Who/what is Orion?";
            ans1.Text = "Original name of the Intersect";
            ans2.Text = "Alias of a secret mission";
            ans3.Text = "Morgan's Xbox";
            ans4.Text = "Chuck's father";
        }

    }

    public void ans1_Click(object sender, EventArgs e)
    {
        if (questionNr == 1)
        {
            pointCounter++;
            pointsLabel.Text = "Current points:" + pointCounter.ToString();
            questionNr++;
        }


    }

    private void ans2_Click(object sender, EventArgs e)
    {
        if (questionNr == 1)
        {
            questionNr++;
        }
    }

    private void ans3_Click(object sender, EventArgs e)
    {
        if (questionNr == 1)
        {
            questionNr++;
        }
    }

    private void ans4_Click(object sender, EventArgs e)
    {
        if (questionNr == 1)
        {
            questionNr++;
        }
    }

}

【问题讨论】:

  • 这在 C 中也行不通
  • 你要这个做什么?

标签: c# loops for-loop compiler-errors


【解决方案1】:

C# 文件的词法结构必须是:

namespace
    class
        method
            statement

for 是一个语句,但您已将它放在方法之外。把它放在方法里面。

【讨论】:

    【解决方案2】:

    不..那行不通。

    您在循环体内声明方法。据我所知,这在 C#.. 或 C 中无效。它肯定不在 C++ 中。

    我很抱歉没有给你一个有效的解决办法。但老实说我看不出你在做什么。

    【讨论】:

    • 那么也许这应该是一条评论?
    【解决方案3】:

    方法不能在循环内声明。尝试将方法移到循环之外,并从循环中调用它们。像这样:

    public void firstMethod()
    {
        for(int i = 0; i<10; i++)
        {
            aMethod();//call the method "aMethod()" 10 times
        }
    }
    //outside of the method containing the loop
    public void aMethod() {
        //do some stuff in the method
    }
    

    【讨论】:

      【解决方案4】:

      您的for 循环似乎包含整个类。试试这个:

      public void question()
      {
      
          for (int i = 1; i <= 10; i++){
              if (questionNr == 1)
              {
                  questionLabel.Text = "What is Chuck's full name?";
                  ans1.Text = "Charles Irving Bartowski";
                  ans2.Text = "Charles Richard Bartowski";
                  ans3.Text = "Charles Luke Bartowski";
                  ans4.Text = "Zachary Strahovski";
              }
              else if (questionNr == 2)
              {
                  questionLabel.Text = "Who/what is Orion?";
                  ans1.Text = "Original name of the Intersect";
                  ans2.Text = "Alias of a secret mission";
                  ans3.Text = "Morgan's Xbox";
                  ans4.Text = "Chuck's father";
              }
          }
      }
      
      public void ans1_Click(object sender, EventArgs e)
      {
          if (questionNr == 1)
          {
              pointCounter++;
              pointsLabel.Text = "Current points:" + pointCounter.ToString();
              questionNr++;
          }
      
      
      }
      
      private void ans2_Click(object sender, EventArgs e)
      {
          if (questionNr == 1)
          {
              questionNr++;
          }
      }
      
      private void ans3_Click(object sender, EventArgs e)
      {
          if (questionNr == 1)
          {
              questionNr++;
          }
      }
      
      private void ans4_Click(object sender, EventArgs e)
      {
          if (questionNr == 1)
          {
              questionNr++;
          }
      }
      

      【讨论】:

      • 虽然这会编译..它似乎没什么用。
      • 我同意。但至少它编译!没有提到i,所以任何人都可以猜测for 循环应该去哪里:)
      • 是的,我在大约 1 小时前看到了他的上一个问题,我很确定我提供的答案会满足他的要求。他根本不需要 for 循环
      【解决方案5】:

      当我看到您之前的问题时,让我详细说明我在那里所说的内容。我确定当用户选择您希望转到下一个问题的答案时。如果是这种情况,只需增加Event Handler 增量questionNr。唯一的规定是如果它是最后一个问题,那么您需要处理该事件(questionNr == 10)。在你的Event Handler 在你increment 之后,然后从之前调用你的Question 方法。在您的Question 方法中,您在questionNr 上有一个switch 语句,并相应地更改textbox 的值。我知道我没有为您提供代码,但我评论了您的第一个问题,所以我引用了您的原始代码。

      好的,我改变了主意,将添加代码:)

      提问方法:

      public void question()
      {
          switch(questionNr)
          {
              case 1:
                  questionLabel.Text = "What is Chuck's full name?";
                  ans1.Text = "Charles Irving Bartowski";
                  ans2.Text = "Charles Richard Bartowski";
                  ans3.Text = "Charles Luke Bartowski";
                  ans4.Text = "Zachary Strahovski";
              break;
              case 2:
                  questionLabel.Text = "Who/what is Orion?";
                  ans1.Text = "Original name of the Intersect";
                  ans2.Text = "Alias of a secret mission";
                  ans3.Text = "Morgan's Xbox";
                  ans4.Text = "Chuck's father";
              break;
              case 3:
              break;
              // More and More Questions.
          }
      }
      

      应答按钮点击事件:

      public void ans1_Click(object sender, EventArgs e)
      {
          questionNr++
          this.question();
          // Whatever else you need to do
      }
      

      【讨论】:

        【解决方案6】:

        在任何一种语言中,您都不能在 for 循环中定义函数。 for 循环和其他控制结构必须在函数内部,函数必须在类内部。

        【讨论】:

          【解决方案7】:

          你可以的

          for (int i = 1; i <= 10; i++){
          
              Action question = () =>
              {
                  //Method Body
          
              };
          
              //...
          }
          

          或者根据你使用的 c# 版本使用委托,但我认为在编写 c# 时最好使用 c# 样式和约定。

          编辑* 很多人说你不能在不正确的 for 循环中使用函数。例如,假设您将处理程序附加到一大组控件,您可以使用 Func 创建一个函数或使用 Action 创建一个方法。参考http://msdn.microsoft.com/en-us/library/System(v=vs.110).aspx

          【讨论】:

            猜你喜欢
            • 2022-01-12
            • 1970-01-01
            • 2014-09-01
            • 2016-08-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-03-12
            • 2017-01-27
            相关资源
            最近更新 更多