0

我是编程新手,我的代码有一些问题:我不太确定那个东西是如何被调用的,所以很难用谷歌搜索它;但我认为人类会理解我的意思:

while 循环每次增加 i++。在我想表达的评论行中

当 i = 1 时, player1.cards[j] = 随机;i = 2 , player2.cards[j] = 随机;

void cardScramble()
{
  int random;
  int i = 1;
  while (i <= 4)
  {
    cout << "Card of Player " << i << " : ";
    int j = 0;
    while (j < 13)
    {
      random = rand() % 52;
      if (cards[random] == NULL)
      {
        cards[random] = i;
        cout << cardName(random) << " , ";
        /*   player(i).cards[j] = random; */
        /* That's what I'm doubt with */
        j++;
      }
    }

    cout << endl;
    i++;
  }
  return;
}

我试图将它定义或操作为字符串,但没有奏效。任何人都可以帮助我吗?非常感谢!

4

2 回答 2

4

你不能按照你想的方式去做。相反,将player1and player2(和任何其他玩家)组合成一个数组。例如:

Player_Type player[2]; // alternatively std::array<Player_Type,2> player;

然后你可以i像这样引用每个玩家:

player[i].cards[j] = random;

或者,如果您想从i1 开始,则只需从中减去 1:

player[i-1].cards[j] = random;
于 2013-02-01T18:58:58.193 回答
1

您可以如下所述使用。您需要一个具有 int 数组的结构。

 struct Player
    {
        public
        int[] cards = new int[13];
    }; 

然后你的 while 循环如下所示

Palyer []player = new Palyer[4];

int random;
        int i = 1;
        while (i <= 4)
        {
            cout << "Card of Player " << i << " : ";
            int j = 0;
            while (j < 13)
            {
                random = rand() % 52;
                if (player[i].cards[random] == NULL)
                {
                    cards[random] = i;
                    cout << cardName(random) << " , ";
                     player[i].cards[j] = random; 
                    /* That's what I'm doubt with */
                    j++;
                }
            }
            i++;
        }

//如果有帮助就点赞

于 2013-02-01T20:04:37.090 回答