C++;设置私有数组的值 我在C++中创建Yahtzee。骰子掷出一个数字,根据掷出的数字,从randNum中减去1即可创建一个位置(因为数组是基于零的)。然后使用这个位置,我想在数组中增加适当的元素,以表示我已经滚动了那个特定的数字。例如:掷骰子1,1,1,5,6,4。我的数组应该显示:3,0,0,1,1,1。在我当前的代码中,元素不是递增的。感谢您的帮助

C++;设置私有数组的值 我在C++中创建Yahtzee。骰子掷出一个数字,根据掷出的数字,从randNum中减去1即可创建一个位置(因为数组是基于零的)。然后使用这个位置,我想在数组中增加适当的元素,以表示我已经滚动了那个特定的数字。例如:掷骰子1,1,1,5,6,4。我的数组应该显示:3,0,0,1,1,1。在我当前的代码中,元素不是递增的。感谢您的帮助,c++,arrays,private,C++,Arrays,Private,标题: class dice { public: dice(); void rollDice(); int getRoll(int diceNumber); //This is the array that tells us how many of one number we have //ex) element 4 belongs to the #5 so if there is a 3 stored in element 4 that means w

标题:

class dice {
public:
    dice();
    void rollDice();
    int getRoll(int diceNumber);

    //This is the array that tells us how many of one number we have
    //ex) element 4 belongs to the #5 so if there is a 3 stored in element 4 that means we rolled 3 5's
    int arrayOfEachNumberRolled[5]{0,0,0,0,0};

    ~dice();
}
资料来源:

void dice::rollDice(){
    //roll a random number and store it in the appropriate location
    int randNum;
    for(int i = 0; i < 6; i++){

        //Get the new random number between 1 & 6
        randNum = rand() % 6 + 1;
        //set the appropriate dice
        if(i == 0){//dice1
            dice1->set(randNum);
            arrayOfEachNumberRolled[randNum - 1]+=1;
        }
        else if(i == 1){//dice 2
            dice2->set(randNum);
            arrayOfEachNumberRolled[randNum - 1]++;
        }
        else if(i == 2){//dice 3
            dice3->set(randNum);
            arrayOfEachNumberRolled[randNum - 1]++;
        }
        else if(i == 3){//dice 4
            dice4->set(randNum);
            arrayOfEachNumberRolled[randNum - 1]++;
        }
        else if(i == 4){//dice 5
            dice5->set(randNum);
            arrayOfEachNumberRolled[randNum - 1]++;
        }
        else if (i == 5){//dice 6
            dice6->set(randNum);
            arrayOfEachNumberRolled[randNum - 1]++;
        }
    }
}
void dice::rollDice(){
//滚动一个随机数并将其存储在适当的位置
int-randNum;
对于(int i=0;i<6;i++){
//获取介于1和6之间的新随机数
randNum=rand()%6+1;
//设置适当的骰子
如果(i==0){//1
骰子1->set(随机数);
arrayOfEachNumberRolled[randNum-1]+=1;
}
如果(i==1){//骰子2
骰子2->set(随机数);
arrayOfEachNumberRolled[randNum-1]++;
}
如果(i==2){//骰子3
骰子3->set(随机数);
arrayOfEachNumberRolled[randNum-1]++;
}
如果(i==3){//骰子4
dice4->set(随机数);
arrayOfEachNumberRolled[randNum-1]++;
}
如果(i==4){//骰子5
dice5->set(随机数);
arrayOfEachNumberRolled[randNum-1]++;
}
如果(i==5){//骰子6
dice6->set(随机数);
arrayOfEachNumberRolled[randNum-1]++;
}
}
}

您需要6个元素的数组,而不是5个元素。改变

int arrayOfEachNumberRolled[5]{0,0,0,0,0};


脸掌表情符号。真不敢相信我错过了。工作得很好。TY@MattCucco,别难过。我们大多数人都去过那里:)
int arrayOfEachNumberRolled[6]{0,0,0,0,0,0};