简体   繁体   English

保龄球比赛的C程序

[英]C program for bowling game

I need to write a program for a game of bowling. 我需要为一场保龄球比赛写一个程序。 Ask the user to enter the number of games and the number of bowlers. 要求用户输入游戏数量和投球手数量。 For each bowler, get the score for each game. 对于每个投球手,获得每场比赛的得分。 Display the score. 显示分数。 Calculate the average for each bowler and display the average. 计算每个投球手的平均值并显示平均值。 Finally display the team average. 最后显示团队平均值。 I wrote a code, it doesn't have errors but the problem is that it doesn't count average score for players and total score for team. 我写了一个代码,它没有错误,但问题是它不计算玩家的平均分数和团队的总分。 Need help with these calculations. 需要这些计算的帮助。

#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;
}

It's nothing to do with "not counting" - you're just not printing them. 它与“不计数”无关 - 你只是不打印它们。 This: 这个:

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

should be: 应该:

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

Note the change from 2 to 2.0 , since playerTotal is an int , but if the total is odd then the average will (or should) have a .5 on the end. 注意从22.0的变化,因为playerTotal是一个int ,但如果total是奇数,则平均值将(或应该)在结尾处有.5

You're also going to want to set playerTotal = 0; 你还想要设置playerTotal = 0; at the start of your outer for loop, otherwise each player is going to get the scores of all the bowlers who entered their scores previously, which is probably not all that fair of a scoring system. 在你的外围for循环的开始,否则每个玩家将获得之前输入他们的分数的所有投球手的分数,这可能不是得分系统的公平。 You should change: 你应该改变:

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

to: 至:

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

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

which will also loop for the same number of times that you had the user enter. 这也将循环您用户输入的次数相同。 If you do this, you'll also need to change: 如果你这样做,你还需要改变:

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

to: 至:

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

to divide your average by the correct number of games. 将你的平均值除以正确的游戏数量。

Other than those things, you made a pretty nice attempt. 除了那些东西,你做了一个非常好的尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM