c# - 与此for循环等效的C#方式是什么?

标签 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++;
        }
    }

}

最佳答案

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

namespace
    class
        method
            statement
for是一个语句,但是您已将其放置在方法之外。将其放入方法中。

关于c# - 与此for循环等效的C#方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21320033/

相关文章:

javascript - 循环遍历 JSON 数组中的每个 x 对象

C - 每当我遇到空格时,循环似乎减半

javascript - 功能恰好影响一半的预期元素

c# for 循环错误的迭代次数

c# - 使用 Linq 和 Lambda 表达式从表中选择多个字段

c# - 防止 child 继承 parent 的上下文菜单

c# - 如何检测防病毒/防火墙是否阻止我的应用程序?

loops - 为迭代定义一个宏

javascript - 使用 jquery 在 HTML 表中追加行不起作用

c# - 工作流程 4.0 和 System.Attribute