上次看到我之前的帖子的人很抱歉。它充斥着粗心的错误和错别字。这是我的任务:
“编写一个程序,使用户能够通过输入语句输入一系列非负数。在输入过程结束时,程序将显示:奇数个数及其平均值;偶数个数“
这是我的代码:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int number, total1=0, total2=0, count1=0, count2=0;
do
{
cout << "Please enter a number. The program will add up the odd and even ones separately, and average them: ";
cin >> number;
if(number % 2 == 0)
{
count1++;
total1+=number;
}
else if (number >= 0)
{
count2++;
total2+=number;
}
}
while (number>=0);
int avg1 = total1/count1;
int avg2 = total2/count2;
cout << "The average of your odd numbers are: " << avg1 << endl;
cout << "The average of your even numbers are " << avg2 << endl;
}
它似乎工作正常,但是当我输入一个负数来终止程序时,它会将它与其余的平均数一起包含在内。有什么建议可以解决这个问题吗?我知道这是可能的,但我没有想到这个想法。