我写了这段代码,我希望它找到负数、最小数、最大数、数字的平均值和数字的总和
它不起作用,你能帮我使用if
语句吗?我找不到最小或最大(当数字为负数时)+ 无法找到所有数字是负数还是正数或混合。
#include <iostream>
using namespace std;
void main() {
float num,
average,
sum,
smallest,
largest,
positive,
negative;
int count;
sum = 0;
count = 1;
largest = 0;
smallest = 0;
negative = 0;
while (count < 11) {
cout << "Number #" << count << ": ";
cin >> num;
if (num < 0) negative++;
sum += num;
average = (sum / 10);
if (num > largest) largest = num;
else if (num < smallest) smallest = num;
count++;
}
cout << endl << "The smallest number is " << smallest << endl;
cout << "The largest number is " << largest << endl;
cout << "The average of all numbers is " << average << endl;
cout << "The sum of all numbers is " << sum << endl;
cout << endl;
cout << "There are " << negative << " negative numbers." << endl;
if (num > 0) cout << "All numbers are positive.";
else if (sum < 0) cout << "All numbers are negative.";
else if {
cout << "There is a mixture of numbers.";
}
}
//------------------------------------ 编辑
我做了一些改变,我几乎是对的,但我仍然找不到找到最小和最大的方法
using namespace std;
void main()
{
float num,
average,
sum,
largest,
smallest,
positive,
negative;
int count;
sum = 0;
count = 1;
negative = 0;
largest= 0;
smallest = 0;
while ( count < 11)
{
cout << "Number #" << count << ": ";
cin >> num;
if ( num < 0 )negative++;
sum += num;
if (count != 0 )
{
if ( num > largest )
largest = num;
else if ( num < smallest )
smallest = num;
}
count++;
}
average = ( sum / 10 );
cout << endl << "The smallest number is " << smallest << endl;
cout << "The largest number is " << largest << endl;
cout << "The average of all numbers is " << average << endl;
cout << "The sum of all numbers is " << sum << endl;
cout << endl;
cout << "There are " << negative << " negative numbers." << endl;
if ( negative == 0 )
cout << "All numbers are positive.";
else if ( negative == 10 )
cout << "All numbers are negative.";
else
{
cout << "There is a mixture of numbers.";
}
}