我試圖製作一個控制檯遊戲,您可以輸入多達24個名稱。爲此,我創建了一個名爲PlayerData
的類的數組,名稱爲PlayerDataAr[]
,其中包含24個元素。它會提示用戶輸入一些名稱,並將這些名稱分配給陣列中的每個元素,其值爲string Name
和bool isAlive
,但由於某種原因,我似乎無法在訪問這些值時將其分配給播放器。C#Array of Classes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hG
{
public class PlayerData
{
private static bool _isAlive;
public static bool isAlive
{
get { return _isAlive;}
set { _isAlive = value;}
}
private static string _Name;
public static string Name
{
get { return _Name; }
set { _Name = value; }
}
public PlayerData()
{
_isAlive = false;
_Name = "";
}
public static void SetisAlive(bool a)
{
_isAlive = a;
}
}
class Program
{
static void Main(string[] args)
{
string EnterName = "";
//Array of Player Data
PlayerData[] PlayerDataAr = new PlayerData[24];
for (int x = 0; x < 24; x++)
{
PlayerDataAr[x] = new PlayerData();
}
//Set up the Console
Console.Title = "Hunger Games";
Console.SetBufferSize(100, 42);
Console.SetWindowSize(100, 42);
Console.BackgroundColor = ConsoleColor.Yellow;
Console.Clear();
Console.ForegroundColor = ConsoleColor.Magenta;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.WriteLine("Welcome");
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Enter the names for tributes and press enter when done:");
//Loop through collecting names
for(int x = 0; x < 25; x++)
{
Console.Write("--> ");
EnterName = Console.ReadLine();
if (EnterName == "")
{
break;
}
else
{
//Assign Player Data
PlayerDataAr[x].Name = EnterName; //Error appears here
PlayerDataAr[x].isAlive = true;
}
}
Console.Clear();
for (int x = 0; x < 24; x++)
{
}
//Start Game
while(true)
{
Console.ReadLine();
}
}
}
}
它返回:
會員 'hG.PlayerData.isAlive.get' 不能與實例引用來訪問;相反,使用類型名稱來限定它。
我不知道它是什麼談論它,這樣對Name
和isAlive
。 任何幫助將不勝感激。
你試過'get {return PlayerData._isAlive;}'? – LInsoDeTeh
您可以通過簡單地查找靜態關鍵字的含義來了解您在代碼中使用的問題。 – nv3