1

我需要为保龄球游戏编写一个程序。要求用户输入游戏数和投球手数。对于每个投球手,获得每场比赛的得分。显示分数。计算每个投球手的平均值并显示平均值。最后显示球队平均水平。我写了一个代码,它没有错误,但问题是它不计算球员的平均得分和球队的总得分。在这些计算方面需要帮助。

#include <stdio.h>

int main()
{
    int playerTotal = 0;
    int teamTotal = 0;
    int score;
    int numgames, player, bowler, game;

    printf ("How many games?\n");
    scanf ("%d", &numgames);
    printf ("How many players?\n");
    scanf ("%d", &player);

    for (bowler = 1; bowler <= 3; bowler++)
       {
        for (game = 1; game <= 2; game++)
           {
        printf ("\nEnter the score for game %d for bowler %d: ", game, bowler);
        scanf ("%d", &score);

        playerTotal += score;
           }
        printf ("\nThe average for bowler %d is: ", bowler, playerTotal/2);

        printf ("\nThe total for the game is:", teamTotal);

        teamTotal += playerTotal;
       }


   return 0;
}
4

1 回答 1

3

这与“不计数”无关 - 你只是不打印它们。这个:

printf ("\nThe average for bowler %d is: ", bowler, playerTotal/2);
printf ("\nThe total for the game is:", teamTotal);

应该:

printf ("\nThe average for bowler %d is: %.1f", bowler, playerTotal / 2.0);
printf ("\nThe running team total is: %d", teamTotal);

注意从2到的变化2.0,因为playerTotal是 a int,但如果总数是奇数,那么平均值将(或应该).5最后有 a。

您还需要playerTotal = 0;在外for循环的开始处设置,否则每个玩家都将获得之前输入分数的所有投球手的分数,这可能不是一个评分系统那么公平。你应该改变:

for (bowler = 1; bowler <= 3; bowler++)
       {
        for (game = 1; game <= 2; game++)
           {

至:

for (bowler = 1; bowler <= player; ++bowler) {
    playerTotal = 0;

    for (game = 1; game <= numgames; ++game) {

这也将循环您让用户输入的相同次数。如果你这样做,你还需要改变:

printf ("\nThe average for bowler %d is: %.1f", bowler, playerTotal / 2.0);

至:

printf ("\nThe average for bowler %d is: %.1f", bowler,
            playerTotal / (double) numgames);

将您的平均数除以正确的游戏数。

除了这些,你做了一个很好的尝试。

于 2013-10-27T04:13:25.493 回答