2013-01-22 31 views
-1

我對C#和麪向對象編程都很新。我想弄清楚爲什麼這個程序不能編譯。下面是一個真實給我的麻煩代碼片段:if-else語句是否出現在C#的Main函數之外?

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' 

這不是在同一個班我主要是如果這是相關的。

+3

我懷疑這段代碼不是問題。這放置在哪裏?之前的代碼是否正確關閉? –

+0

如果不知道此片段的上下文,則無法給出答案。 –

+0

爲什麼你有一個'void'的return語句? – Gabe

回答

3

正如其他人所說,你需要在這種情況下顯示你所有的代碼,因爲它聽起來像別的東西在你的代碼中缺少/錯誤,並在這一點上突破。這裏有一個你正在嘗試做的事情的例子,雖然它看起來不是一個很好的方法,有很多static項目,你應該看看Object Oriented Programming

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

這個。 C#要求所有的方法,甚至是靜態方法都在類定義中,類似於Java;在C/C++中沒有像真正的「全局」函數那樣的東西。這些錯誤表明你的方法只是在代碼文件中自行懸掛;沒有es bueno。 – KeithS

1

幾件事情,

  1. 你的方法被聲明爲無效,並返回更新,因此使該方法相同的返回類型的更新。
  2. 如果您的方法與main不在同一個類中,則將該方法設爲public,並在包含main的類中引用新類。
  3. 在使用它們之前聲明XP和級別爲可變參數,並在方法級別範圍內聲明它們。
  4. 此方法應與更新在同一個類中。