【问题标题】:A type but is used like a variable?一种类型,但像变量一样使用?
【发布时间】:2013-12-17 13:45:06
【问题描述】:
    struct Player
    {
        public string Name;
        public int X;
        public int Y;
    }
    static Player[] players = new Player[amountofPlayers];

        for (int i = 1; i < amountofPlayers; i = i + 1)
        {
            int displayNumber = i + 1;
            Console.Write("Please enter the name of player " + displayNumber + ": ");
            Player[i].Name = Console.ReadLine(); // The error is here

        }

谁能帮我解决这个问题,因为我看不出哪里出错了...

【问题讨论】:

  • 这是拼写错误,使用players[i] 而不是Player[i].. 最好使用foreach

标签: c# variables types


【解决方案1】:

Player 是类型/结构,players Player[],所以使用这个:

players[i].Name = Console.ReadLine(); // instead of Player[i]

【讨论】:

  • 感谢闪电般的快速响应:P,如果我将代码更改为您建议的方式,它将不再将数据存储在结构中,知道如何解决这个问题吗?没关系。
  • @user3055553:我必须承认我不明白你的意思。您如何将数据存储在结构中?变量playersPlayer[] 之外的Player,现在和以前。
  • 我明白你的意思并理解我哪里出错了,很抱歉造成混乱。哈哈
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 2020-05-28
  • 1970-01-01
相关资源
最近更新 更多