您好,我正在尝试学习编程方法,我希望用户输入有多少名球员以及他们的名字。我看完一些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;
}

最佳答案

您要输入播放器编号。您似乎已经编写了用于输入玩家姓名的代码。因此,要输入播放器数量,您需要使用一个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;
}


希望这可以帮助 :)

关于c - C如何获得球员人数及其姓名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44360569/

10-09 13:26