0

我写了这段代码,我希望它找到负数、最小数、最大数、数字的平均值和数字的总和

它不起作用,你能帮我使用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.";
 }
}
4

1 回答 1

2

在获得所有数字之前,您不应该计算平均值;或者如果你想保持一个运行平均值,你必须除以你到目前为止的总和。所以代替线

average = sum / 10;

在循环的中间,尝试

average = sum / count;

while 在循环内,或

average = sum / (count - 1);

循环之后。当然,由于在循环完成之前您实际上并没有使用平均值,所以这实际上并不重要 - 但它有点草率。

接下来,您的代码

if ( num > 0 )
   cout << "All numbers are positive.";
else if ( sum  < 0 )
   cout << "All numbers are negative.";

有两个明显的错误。首先-您检查“num”是否为正-这是输入的最后一个数字,而不是“所有数字”。如果您有一个非常负的数字,则总和将为负数。而是使用您的负数计数:

if ( negative == 0) // all positive
if ( negative == 10) // all negative

否则,它是一个混合物。我相信您可以使用这些指针修复您的代码。

编辑#2

我决定在一个完整的程序中实现我上面提到的所有更改,以及我在评论中提到的更改。我最终得到的代码是这样的:

#include <iostream>
using namespace std;

int main()
{

float num,
  average,
  sum=0,
  largest=0,
  smallest=0,
  positive=0,
  negative=0;

int count=1;

negative = 0;

while ( count < 11)
{
  cout << "Number #" << count << ": ";
  cin >> num;

  if ( num < 0 ) negative++;

  sum += num;

  if (count == 1) {
    largest = num;
    smallest = num;
  }
  else {
     if ( num > largest )
        largest = num;
     if ( num < smallest )
        smallest = num;
  }

  count++;
}

average = ( sum / (count - 1) );


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."<<endl;
else if ( negative == 10 )
  cout << "All numbers are negative."<<endl;
else
{
  cout << "There is a mixture of numbers.";
}

}

这编译得很好;当我运行它并输入以下输入时:

Number #1: 5
Number #2: 4
Number #3: 3
Number #4: 2
Number #5: 1
Number #6: 0
Number #7: -1
Number #8: -2
Number #9: -3
Number #10: -4

我得到这个输出:

The smallest number is -4
The largest number is 5
The average of all numbers is 0.5
The sum of all numbers is 5

There are 4 negative numbers.
There is a mixture of numbers.

在我看来它正在工作。对你起作用吗?

于 2013-04-25T01:06:35.407 回答