[英]C program for bowling game
我需要為一場保齡球比賽寫一個程序。 要求用戶輸入游戲數量和投球手數量。 對於每個投球手,獲得每場比賽的得分。 顯示分數。 計算每個投球手的平均值並顯示平均值。 最后顯示團隊平均值。 我寫了一個代碼,它沒有錯誤,但問題是它不計算玩家的平均分數和團隊的總分。 需要這些計算的幫助。
#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;
}
它與“不計數”無關 - 你只是不打印它們。 這個:
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
,但如果total是奇數,則平均值將(或應該)在結尾處有.5
。
你還想要設置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);
將你的平均值除以正確的游戲數量。
除了那些東西,你做了一個非常好的嘗試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.