我正在开始计算机科学课程,我们目前正在学习功能,但我认为我会更进一步,并在早期开始自我教授对象。我尝试制作这个平均游戏分数计算器,但输入值10,20,30和40然后输入循环标记值-999。我应该得到平均25分,因为它是100/4,我得到80分。我已经玩了很多但是我无法弄清楚我做错了什么 - 我试着不除非在通过谷歌和我的书阅读几个小时之后我真的无法解决问题,否则不再寻求帮助。所以任何帮助将非常感谢:)
编辑:我的问题是:为什么当我输入所有这些值时,他们没有通过正确的路线获得正确的平均值? (这是我第一次尝试使用物品并且不太了解如何使用它们,但我认为试验和错误会帮助我学习。)
先谢谢你了!
P.S我是新来的,但我希望我正确地格式化这个问题,以免让任何人再次对我生气。哦,我知道我还没有使用iomanip或字符串库,但是我计划将来所以我只是将它们留在那里。
//COMSCI 110
//HUNTER DURNFORD
//INPUT
//SPECIAL COMPILER FUNCTIONS
//NONE
//PROCESSING
//
//DATA
//
//LIBRARIES
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;
//PROGRAMMER DEFINED FUNCTIONS
//introduction
//
//Object Classes
//AvgScoreCalculator
//Object class
class AvgScoreCalculator
{
private:
//double total_Scores = game_Scores;// The total scores
//double score_Average = total_Scores / score_Count; // the average equation
public:
double total_Scores; // the total that gets input into the average function
double score_Average; // the average of all the games
double score_Count; // the counter for each individual game entry
double game_Scores; // the scores for the individual games
//char exit_Value;
};//end class
//introduction function
void introduction()
{
cout << "Enter any amount of characters and this program will encrypt them." << endl;
cout << "By Hunter Durnford." << endl;
cout << "Editor(s) used: XCODE TEXT EDITOR and JNotePad 2\n";
cout << "Compiler(s) used: XCODE\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
}//introduction
//function to get scores from user
double getScoreFunction()
{
double score_Count = 0.0;
double gameScores = 0.0;
double total_Scores = 0.0;
while(true)
{
cout << "What score would you like to input? Or type -999 to exit. ";
cin >> gameScores;
if(gameScores == -999)
{
break;
}
score_Count = score_Count + 1;
gameScores = gameScores + gameScores;
total_Scores = gameScores;
}
return total_Scores;
}//end score function
//Average of all the scores function
double getAverageFunction(double total_Scores, double score_Count)
{
double average_Score;
average_Score = total_Scores / score_Count;
return average_Score;
}
//main program
int main()
{
introduction();
AvgScoreCalculator average_Score; //Declaring the object under the class
//set the integers for the average_score object
average_Score.score_Count = 0.0;
average_Score.game_Scores = 0.0;
average_Score.score_Average = 0.0;
average_Score.total_Scores = 0.0;
average_Score.game_Scores = getScoreFunction();
average_Score.score_Average = getAverageFunction(average_Score.total_Scores, average_Score.score_Count);
cout << "The average of all the game scores is: " << average_Score.game_Scores << endl;
}
答案 0 :(得分:0)
(将评论中指出的双精度问题分开)
问题在这里:
score_Count = score_Count + 1;
gameScores = gameScores + gameScores;
total_Scores = gameScores;
在脑海中播放:
应该是:
score_Count = score_Count + 1;
total_Scores += gameScores;
此外,score_Count是getScoreFunction()
的局部变量...因此其更改不会影响用于平均计算的average_Score.score_Count
。