我有20名球员,每位球员可以投票3次,共20名球员之一。如果第一个输入“数字”是10,我怎样才能将投票添加到userToVote [10] [vote],这是数组中的第10位。
#include <iostream>
#include <string>
using namespace std;
int userToVote[20][3];
int vote,number;
int main()
{
for(int i = 0;i<20;i++)
{
for(int z = 0;z<3;z++)
{
cout << "Hello player "<< i << "Insert the id you want to vote and the vote (0 or 1) ";
cin >> number >> vote;
cout << userToVote[number][vote];
}
}
}
因为player
可以投票3次。你可以在每次投票时增加userToVote[number][vote]
。这样,投票就可以增加。你可以像这样改变你的代码。
cout << "Hello player "<< i << "Insert the id you want to vote and the vote (0 or 1) ";
cin >> number >> vote;
userToVote[number][vote]+=1;
cout << userToVote[number][vote];
.
#include <iostream>
#include <string>
using namespace std;
int userToVote[20]={0};
int number;
int main()
{
for(int i = 0;i<20;i++)
{
for(int z = 0;z<3;z++)
{
cout << "Hello player "<< i << " This is your chance "<<z+1<<" to which player you want to vote?";
cin >> number;
cout << userToVote[number]++;
}
}
}