这已经有一段时间了(去年的Java类)。自从我的学校没有提供它以来,一直试图自己学习C++。我写了一个简单的程序来测试我到目前为止学到的东西 - 真正的语法 - 在我进入中间件之前。无论如何,我只想强调我从来没有找到答案,我宁愿你问我关于我的后勤问题,以便我可以重新思考问题,并可能自行完成。我认为,因为我可以在Java中成功编写这个代码,所有代码在C++中都会很好,但是我遇到了可变的问题。我试图调试和步入,但我仍然不明白为什么我的一些变量没有得到我分配的值。如果你能指出我正确的方向,我会非常感激。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);
}
请列举这导致问题的变量,你期待什么样的价值他们有什么价值观,他们实际上拥有。 – Philipp
尝试阅读std :: vector或http://isocpp.org/tour。 C++不是Java,因为您从实验中看到 –
make int team [0]; - > int团队[100];一个用户只能输入一个小于100的数字,平均值应该是总数/队伍数量。然后你在得分输出中有硬编码队伍[0] – sethi