私は 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'
関連する場合、メインと同じクラスではありません。