我需要为一场保龄球比赛写一个程序。要求用户输入游戏数量和投球手数量。对于每个投球手,获得每场比赛的得分。显示分数。计算每个投球手的平均值并显示平均值。最后显示团队平均值。 我写了一个代码,它没有错误,但问题是它不计算玩家的平均分数和团队的总分。需要这些计算的帮助。
#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;
}
答案 0 :(得分: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
是int
,但如果总数是奇数,则平均值将(或应该)具有.5
最后{1}}。
您还希望在外部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);
将您的平均值除以正确的游戏数量。
除了那些事情,你做了一个非常好的尝试。