已经有一段时间了(去年的 Java 课)。因为我的学校不提供 C++,所以我一直在尝试自己学习 C++。我写了一个简单的程序,只是为了测试我到目前为止所学的东西——实际上只是语法——在我进入中间的东西之前。无论如何,我只想强调我从不寻找答案,我宁愿你问我的后勤问题,这样我就可以重新考虑事情并可能自己完成它。我认为既然我可以用 Java 成功地编写它,那么在 C++ 中一切都会很好,但我遇到了变量问题。我尝试调试并逐步完成,但我仍然不明白为什么我的一些变量没有得到我分配给它们的值。如果您能指出我正确的方向,我将不胜感激。
// This program will create any number of teams the user chooses,
// give each a score and calculate the average of all the teams.
#include <iostream>
using namespace std;
int main(){
//number of teams
int teamCount;
//array to keep scores
int team[0];
//total of scores
int total=0;
//average of all scores
int average=0;
cout<<"How many teams do you want to keep scores of?"<<endl;
cin>>teamCount;
//cout<<teamCount;
//ask the person for the score as many time
//as there are teams.
for(int i=0; i<teamCount; i++){
cout<< "Give me the score of team "<< i+1<<":"<<endl;
cin>>team[i];
total+=team[i];
}
average = teamCount/total;
//output the list of the scores
for(int i=0; i<teamCount; i++){
cout<<"Team "<<i+1<<" score is:"<<team[0]<<endl;
}
cout<<"and the average of all scores is "<<average<<endl;
return (0);
}