所以,我得到這個作爲一個錯誤存在:變量不會在目前情況下
The name
p1
does not exist in the current context.
它指的是代碼行中Attack()
在Player
類。
namespace GameOne
{
public class Program
{
public static void Main()
{
Ai [] badguys = new Ai [5];
Player p1 = new Player();
}
}
public class Ai
{
public Ai()
{
int Health = 10;
int Damage = 1;
}
public static int Attack()
{
p1.Health--;
return p1.Health;
}
}
public class Player
{
public Player()
{
int Health = 50;
}
}
}
'p1'是'Main()'中的局部變量。 'Attack()'沒有定義一個名爲'p1'的符號。 – xxbbcc
當您發佈代碼時,請仔細檢查並確保所有代碼格式正確,縮進一致,並刪除多餘的空行以提高可讀性。 – crashmstr
總是嘗試在詢問之前在線搜索錯誤,以下文章可能會有幫助: https://stackoverflow.com/questions/41974155/the-name-does-not-exist-in-the-current-context –