结果不正确-C编程

大家好。 我有个问题。如何修复我的不正确结果,如下图所示,在我的“获得点”零件第1轮中,结果不正确?答案是:玩家1为34,玩家2为45,玩家3为50。

另外,我也知道fflush(stdin)不适合在编码中实现。但是当我删除fflush(stdin)时,fgets跳过了我的输入。谁能建议我如何解决此问题?

这是我的代码:

#include<stdio.h>

int main(void)
{

int round;
int player;
char name1[30];
char name2[30];
char name3[30];
int total1;
int total2;
int total3;
int times;
int point[3];

total1=0;
total2=0;
total3=0;

printf("======League of Legends======\n");

printf("How many round?:");
scanf("%d", &round);

printf("How many player:");
scanf("%d", &player);

fflush(stdin);

printf("Name player 1:");
fgets(name1, 30, stdin);

printf("Name player 2:");
fgets(name2, 30, stdin);

printf("Name player 3:");
fgets(name3, 30, stdin);

for(times=1; times<=round; times++)
{
    printf("## Round %d ##\n", times);
    printf("Point for player 1(%s):",name1);
    scanf("%d", &point[0]);

    printf("Point for player 2(%s):", name2);
    scanf("%d", &point[1]);

    printf("Point for player 3(%s):", name3);
    scanf("%d", &point[2]);

    total1=total1+point[0];
    total2=total2+point[1];
    total3=total3+point[2];

}

 for(times=1; times<=round; times++)

 {
    printf("******Point obtained******\n");

    printf("## Round %d ##\n", times);
    printf("Point for player 1(%s): %d\n", name1,  point[0]);

    printf("Point for player 2(%s): %d\n", name2,  point[1]);

    printf("Point for player 3(%s): %d\n", name3,  point[2]);

 }


printf("++++++TOTAL POINT++++++\n");
printf("Total point player 1(%s):%d\n", name1, total1);
printf("Total point player 2(%s):%d\n", name2, total2);
printf("Total point player 3(%s):%d\n", name3, total3);

return 0;
}

enter image description here

评论