2013-01-22 30 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. 此方法应与更新在同一个类中。