C# 大括号问题

发布于 2024-12-25 10:12:32 字数 674 浏览 3 评论 0原文

我正在学习有关制作基于文本的游戏的 C# 教程,但一开始就遇到了问题。以下代码:

namespace GameV2
{
    class Level
    {
        private static Room[,] rooms;

        #region Properties
        public static Room[,] Rooms
        {
            get { return rooms; }
        }
        #endregion


        public static void Initialize();
    *{*
    }

        private static *BuildLevel*();
    {
    }
        return false;


    }
*}*

给了我 3 个错误。

错误 1 ​​类、结构或接口成员声明中的标记“{”无效

错误 2 需要类、委托、枚举、接口或结构
错误 3 类型或命名空间定义,或预期文件结尾

斜体表示按顺序排列的错误。由于某些原因,Visual C# Express 不允许我在方法定义中使用 { ,并将最终的 } 推出代码框。关于为什么会发生这种情况有什么想法吗?

I'm following a tutorial on C# on making a text-based game and I ran into an issue right at the start. The following code:

namespace GameV2
{
    class Level
    {
        private static Room[,] rooms;

        #region Properties
        public static Room[,] Rooms
        {
            get { return rooms; }
        }
        #endregion


        public static void Initialize();
    *{*
    }

        private static *BuildLevel*();
    {
    }
        return false;


    }
*}*

gives me 3 errors.

Error 1 Invalid token '{' in class, struct, or interface member declaration

Error 2 Expected class, delegate, enum, interface, or struct
Error 3 Type or namespace definition, or end-of-file expected

The italics represent the errors in order. Fr some reason Visual c# express won't let me use { in a method definition, and pushes my final } out of the code box. Any ideas on why this happens?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

惯饮孤独 2025-01-01 10:12:32
  • 方法后面没有分号。您可能会将它们与 C 混淆
    方法原型。

  • BuildLevel 应该有一个返回类型。

  • 所有语句都必须位于方法内部,只能在方法外部进行声明

这应该编译:

namespace GameV2
{
    class Level
    {
        private static Room[,] rooms;

        #region Properties
        public static Room[,] Rooms
        {
            get { return rooms; }
        }
        #endregion


        public static void Initialize()
        {
        }

        private static bool BuildLevel()
        {
            return false;
        }

    }
}
  • You don't have semicolons after methods. You may be confusing them for C
    method prototypes.

  • BuildLevel should have a return type.

  • All statements have to be inside methods, you can only have declarations outside of methods

This should compile:

namespace GameV2
{
    class Level
    {
        private static Room[,] rooms;

        #region Properties
        public static Room[,] Rooms
        {
            get { return rooms; }
        }
        #endregion


        public static void Initialize()
        {
        }

        private static bool BuildLevel()
        {
            return false;
        }

    }
}
两相知 2025-01-01 10:12:32
public static void Initialize();

private static *BuildLevel*();

这些是声明。它们后面不能跟{ }。删除 ; 就可以了。

private static TYPEHERE *BuildLevel*();

这缺少返回类型。

public static void Initialize();

private static *BuildLevel*();

Those are declarations. They cannot be followed by { }. Remove the ; and it will work.

private static TYPEHERE *BuildLevel*();

This is missing a return type.

花之痕靓丽 2025-01-01 10:12:32

去掉两个;

public static void Initialize()
{
}

private static BuildLevel()
{
}

Remove the two ;

public static void Initialize()
{
}

private static BuildLevel()
{
}
空气里的味道 2025-01-01 10:12:32
public static void Initialize();
{
}

应该是

public static void Initialize()
{
}
public static void Initialize();
{
}

should be

public static void Initialize()
{
}
燕归巢 2025-01-01 10:12:32

从函数声明末尾(左大括号之前)删除分号。

Remove the semicolons from the end of your function declarations (before the opening curly brace).

哎呦我呸! 2025-01-01 10:12:32

注意分号。方法名称与其主体之间有分号。

Watch out for the semicolons. You have semicolons between the method names and their bodies.

夏末的微笑 2025-01-01 10:12:32

这可能是问题所在:

private static *BuildLevel*();
    {
    }
        return false;

您没有指定返回类型,并且 return false; 应该位于括号内。

This may be the problem:

private static *BuildLevel*();
    {
    }
        return false;

You didn't specify a return type, and the return false; should be inside the brackets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文