【发布时间】:2012-02-11 22:11:17
【问题描述】:
我的任务是计算保龄球平均值。我有五名球员,每个球员三场比赛。我目前正在运行两个循环,一个用于播放器,另一个用于游戏编号。我需要显示每个循环结束时球员的平均水平,以及该循环结束时球队的平均水平。
我修复了我的代码,并用下面的新代码替换了我的旧代码。在我在这里查看每个人的 cmets 等之前,我一直在玩它,到那时我已经解决了。
但是谢谢大家!
#include <iostream>
using namespace std;
int main()
{
//DECLARATIONS
const int PLAYER_NUMBER = 5; //There are five players total
const int GAME_NUMBER = 3; //There are three games total
const int MIN = 0; //Min number
const int MAX = 300; //Max number
double* playerScore; //The players' score of current game
double playerAverage = 0; //The current players' average
double teamAverage = 0; //The teams' average
//INPUT
for (int currentPlayer = 0; currentPlayer < PLAYER_NUMBER; currentPlayer++)
{//Set the current player number
for (int currentGame = 0; currentGame < GAME_NUMBER; currentGame++)
{//Set the current game number
//Get scores
cout << "For Player " << (currentPlayer + 1) << ", enter score for game " << (currentGame + 1) << ": ";
cin >> playerScore[currentGame];
if(playerScore[currentGame] < MIN || playerScore[currentGame] > MAX)
{//Check range
cout << "The score must be between 0 and 300!\n";
currentGame--; //If there is an error, subtract the game number by one
}//End If statement
playerAverage += playerScore[currentGame];
if(currentGame == 2)
{//Current player average
cout << endl << "The average for player " << (currentPlayer + 1) << " is: " << (playerAverage / 3) << endl << endl;
teamAverage += playerAverage;
playerAverage = 0;
}//End If statement
}//End game for-statement
}//End player for-statement
cout << endl << "The average for the team is: " << (teamAverage / 15) << endl << endl;
//ENDING
system("Pause");
return 0;
}//Close main
但是,对于还在那里的任何人,有没有办法让终端保持打开状态,而不必使用“sys(“PAUSE”);”?我真的很讨厌使用它。
【问题讨论】:
-
如果 cin 执行成功,它下面的行就会执行。我怀疑您看到了一些调试器工件,它看起来像是 cin 正在执行,而不是下面的行。
-
谢谢大家!在我发布这个之后,我正在摆弄代码,我通过移动一些行以某种方式让它工作。我想这可能是侥幸。下次我去编写这样的代码时,我一定会搞砸的:P
-
有时问题只是你忘记保存源文件,或者在你重建东西时它没有自动重新编译。这些事情可能非常令人沮丧。
-
我肯定会说 Dev-C++ 内置的调试器很糟糕。我希望它可以像 Visual Studio 一样向我展示价值和东西。可能有,我没仔细看。是的,事情肯定会变得非常令人沮丧,但到目前为止,C++ 比上学期的 C# 容易得多。