簡體   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