什么是C#方式相当于这个for循环?

时间:2014-01-23 21:46:58

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

我是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++;
        }
    }

}

7 个答案:

答案 0 :(得分:7)

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

namespace
    class
        method
            statement

for是一个声明,但您已将其放在方法之外。把它放在方法中。

答案 1 :(得分:5)

不......那不行。

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

我很抱歉没有给你一个有效的解决办法..但老实说,我看不到你想要做什么。

答案 2 :(得分: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
}

答案 3 :(得分:2)

你的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++;
    }
}

答案 4 :(得分:2)

在我看到你之前的问题时,让我详细说明我在那里所说的内容。我确信当用户选择您想要转到下一个问题的答案时。如果是这种情况,则只需Event Handler增量questionNr。唯一的规定是,如果它是最后一个问题,那么你需要处理该事件(questionNr == 10)。在Event Handler之后的increment中,然后从之前调用Question方法。在Question方法中,您在switch上有一个questionNr语句,并相应地更改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
}

答案 5 :(得分:1)

在两种语言中,都不能在for循环中包含函数定义。 For循环和其他控制结构必须在函数内部,函数必须在类中。

答案 6 :(得分:1)

你可以做到

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

相关问题