2013-02-01 85 views
0

我是編程新手,我的代碼存在一些問題: 我真的不確定這個東西是如何調用的,所以很難谷歌它; 但我認爲人類會理解我的意思:操縱一個字符串到一個變量的名字?

while循環每次增加i ++。在我要表達的評論欄中

當 i = 1時,player1.cards [j] = random; i = 2,player2.cards [j] = random;

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; 
} 

我試圖定義它或將其作爲字符串操作,但沒有奏效。 任何人都可以幫助我嗎?非常感謝!

+0

你必須創建對象的數組來做到這一點。 – Arpit

回答

4

你不能像你想的那樣去做。相反,將player1player2(和任何其他玩家)組合成一個數組。例如:

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

然後你可以使用i這樣是指每個玩家:

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

或者,如果你想從1開始i,那麼就從它減去1:

player[i-1].cards[j] = random; 
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++; 
     } 

//一樣的答案,如果它是有幫助的

+0

是否區分大小寫(變量「Player」)? – Rouvis

+0

玩家是結構。後來我宣佈了Palyer的一個結構數組,並命名爲player。 – GAP

相關問題