您好我正在尝试学习如何编程,我希望用户输入有多少玩家以及他们的名字。在看了一些youtube视频并在网上寻找其他地方之后,我认为我的以下代码是正确的,但我看不出我哪里出错了。如果有人能帮忙那就太棒了。
int main() {
int i, player_num;
char names[8][25];
printf("\n\n");
//user inputs value of player_num, here, as you have now
for(i = 0; i < player_num; i++) {
printf("Enter the player's name: ");
scanf("%s", names[i]); //enters name and creates a newline <enter key>
getchar(); //removes the newline from the keyboard buffer
}
printf("\n\n");
for(i = 0; i < player_num; i++)
printf("\n%s", names[i]);
printf("\n\n\t\t\t press enter when ready");
getchar(); //holds the console window open until you press enter
return 0;
}
答案 0 :(得分:0)
您想输入播放器的数量。您似乎已经编写了输入玩家姓名的代码。因此,要输入播放器的数量,您需要使用一个scanf()
int main()
{
int i, player_num;
char names[8][25];
printf("\n\n");
//user inputs value of player_num, here, as you have now
printf("Enter the number of player(from 1 to 8)\n");
scanf_s("%d",&player_num,sizeof(int));
for(i = 0; i < player_num; i++) {
printf("Enter the player's name: ");
scanf_s("%s", names[i],25); //enters name and creates a newline <enter key>
getchar(); //removes the newline from the keyboard buffer
}
printf("\n\n");
for(i = 0; i < player_num; i++)
printf("\n%s", names[i]);
printf("\n\n\t\t\t press enter when ready");
getchar(); //holds the console window open until you press enter
return 0;
}
希望这有助于:)