如何从该名称列表中打印或访问值?

时间:2019-02-10 19:18:00

标签: c# class console

我正在尝试使用C#创建一个简单的控制台纸牌游戏。创建球员后,我很难弄清楚如何访问他们的名字。

我正在尝试创建一个对象数组,将每个玩家存储在不同的索引中。随着游戏的发展,我将向玩家类添加更多属性(例如剩余的纸牌)。

我希望能够显示球员的姓名,身份证号码等,但不确定如何打印该值。我已经尝试过Console.WriteLine(playerList[i]),但这只是打印名称空间和类。我也无法执行类似Console.writeLine(playerList[i].name)的操作,但这也不起作用。

任何帮助或反馈都很棒!谢谢!

class Player
{
    public int id;
    public string name;
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hi, enter how many players are playing");
        int numOfPlayer = int.Parse(Console.ReadLine());
        Player[] playerList = CreatePlayers(numOfPlayer);

        //What I'm trying to do! >>
        //Console.WriteLine(playerList[0].name + "'s turn")
    }

    static Player[] CreatePlayers(int amountOfPlayers)
    {
        Player[] playerList = new object[amountOfPlayers];

        for (int i = 0; i < amountOfPlayers; i++)
        {
            Console.WriteLine("Enter a name for player " + (i + 1));
            string playerName = Console.ReadLine();
            Player player = new Player();
            player.id = i;
            player.name = playerName;
            playerList[i] = player;
        }
        return playerList;
    }
}

编辑: 我已经将Object数组更改为player数组,并在上面的代码中对其进行了更新。我现在似乎可以通过编写Console.Writeline(playerList [0] .name.ToString())来访问名称。这是一个不错的方法吗?

2 个答案:

答案 0 :(得分:0)

object替换为Player
将类强制转换为object将无法引用原始类的成员。

class Player
{
    public int id;
    public string name;
}


class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hi, enter how many players are playing");
        int numOfPlayer = int.Parse(Console.ReadLine());
        Player[] playerList = CreatePlayers(numOfPlayer);

        //What I'm trying to do! >>
        Console.WriteLine(playerList[0].name + "'s turn");

        Console.ReadKey();
    }

    static Player[] CreatePlayers(int amountOfPlayers)
    {

        Player[] playerList = new Player[amountOfPlayers];

        for (int i = 0; i < amountOfPlayers; i++)
        {
            Console.WriteLine("Enter a name for player " + (i + 1));
            string playerName = Console.ReadLine();
            Player player = new Player();
            player.id = i;
            player.name = playerName;
            playerList[i] = player;
        }
        return playerList;
    }
}

答案 1 :(得分:0)

 class Player
{
    public int id {get;set;}
    public string name{get;set;}
}


class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hi, enter how many players are playing");
        int numOfPlayer = int.Parse(Console.ReadLine());
        List<Player> playerList = CreatePlayers(numOfPlayer);

        //What I'm trying to do! >>
        Console.WriteLine(playerList.ElementAt(0).name + "'s turn");
        Console.ReadLine();

    }

   static List<Player> CreatePlayers(int amountOfPlayers)
    {

        List<Player> playerList = new List<Player>();

        for (int i = 0; i < amountOfPlayers; i++)
        {
            Console.WriteLine("Enter a name for player " + (i + 1));
            string playerName = Console.ReadLine();
            Player player = new Player();
            player.id = i;
            player.name = playerName;
            playerList.Add(player);
        }
        return playerList;
    }
}

最好将集合用于linq或通用集合的任何其他优点。