繁体   English   中英

if-else语句能否出现在C#的Main函数之外?

[英]Can if-else statements appear outside the Main function in C#?

我对C#和面向对象的编程都很陌生。 我试图弄清楚为什么该程序无法编译。 这是给我麻烦的代码片段:

static void AdvancePreLvlThree() // advances player to next level when level is less than three
{
    if(level == 1)
    {
        xp = (xp - 100); // carries remaining xp over to next level
    }
    else if(level == 2)
    {
        xp = (xp - 150);
    }

    level +=1; // advances
    return Update(); // checks again to see if they can advance further
}

完整程序:

///////////////////////////////////////////////////////
//
// A namespace/program for managing experience points 
// and determining if the player can level up.
//
// Written by Jared Beach
// January 19, 2013
//
//////////////////////////////////////////////////////

using System;
namespace LevelSystem
{
    public class LevelSystem 
    {
        public void LevelStorage()
        {
            int level = 1; // stores player level
        }

        public void XPStorage()
        {
            int xp = 0; // stores player xp
        }

        public void XPRequirement() // xp required to advance to the next level
        {
            int maxXP = (8 * level^3);

        static void AdvancePreLvlThree() // advances player to next level when level is less than three
        {
            if(level == 1)
            {
                xp = (xp - 100); // carries remaining xp over to next level
            }
            else if(level == 2)
            {
                xp = (xp - 150);
            }

            level +=1; // advances
            return Update(); // checks again to see if they can advance further
        }

        static void Advance() // advances player to next level
        {
            xp = (level - maxXP); // carries remaining xp over to next level
            level +=1;
            return Update();
        }

        static void Update() // checks to see if player can advance levels
        {
            if(level == 1 && xp > 100) // special case to keep basic progression ratio close to one
            {
                AdvancePreLvlThree();
            }
            else if(level == 3 && xp > 150)
            {
                AdvancePreLvlThree();
            }
            else if(xp >= 3 && xp > maxXP) 
            {
               return Advance();
            }
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {

        }
    }
}

我得到的错误是:

Program.cs(30,8): error CS1525: Unexpected symbol `static'
Program.cs(30,16): error CS1547: Keyword `void' cannot be used in this context
Program.cs(30,38): error CS1525: Unexpected symbol `('
Program.cs(36,16): error CS1519: Unexpected symbol `else' in class, struct, or interface member declaration
Program.cs(36,28): error CS1519: Unexpected symbol `==' in class, struct, or interface member declaration
Program.cs(38,20): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
Program.cs(38,26): error CS1519: Unexpected symbol `-' in class, struct, or interface member declaration
Program.cs(41,12): error CS1525: Unexpected symbol `level'

如果相关的话,它与我的Main不在同一个班级。

正如其他人所说,在这种情况下,您需要显示所有代码,因为听起来您的代码中缺少/错了其他东西,并在那时中断了。 这是您正在尝试执行的工作示例,尽管它看起来不像是具有大量static项的很好方法,并且您应该真正研究面向对象的编程

namespace ConsoleApplication1
{
    class Program
    {
        private static int level = 0;
        private static int xp = 0;

        static void Main(string[] args)
        {
            AdvancePreLvlThree();
        }

        static bool AdvancePreLvlThree() // advances player to next level when level is less than three
        {
            if(level == 1)
            {
                xp = (xp - 100); // carries remaining xp over to next level
            }
            else if(level == 2)
            {
                xp = (xp - 150);
            }

            level +=1; // advances
            return Update(); // checks again to see if they can advance further
        }

        static bool Update()
        {
            // Yes they can...
            return true;
        }
    }
}

我已经使您的AdvancePreLvlThree方法返回布尔值,因为这是您要尝试执行的操作,但是该方法被标记为无效? 无论如何,希望这可以帮助您入门?

一些事情,

  1. 您的方法被声明为void,并且它返回更新,因此使该方法的返回类型与update相同。
  2. 如果您的方法与main不属于同一类,则将方法公开,请在包含main的类中引用新类。
  3. 在使用XP和Level之前,请先声明它们为变量,然后在方法级别范围内声明它们。
  4. 此方法应与update在同一类中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM