【问题标题】:Error in displaying the largest and smallest number显示最大和最小数字时出错
【发布时间】:2012-09-14 20:57:30
【问题描述】:

我试图为如何编程中的练习 2.19 编写一个程序,但遇到了一些困难。

程序应该让用户输入三个整数,然后显示这些整数中的sumaverageproduct

我遇到的唯一问题是显示最大和最小。当我运行程序并输入三个整数(8, 9, and 10)时,输出为Smallest is 8ANDSmallest is 9

我希望你能告诉我原因。

#include <iostream>
using namespace std;

int main ()
{   int x, y, z, sum, ave, prod;

    cout << "Input three different integers ";
    cin >> x >> y >> z;

    sum = x + y + z;
    cout << "\nThe sum is " << sum;

    ave = (x + y + z) / 3;
    cout << "\nThe average is " << ave;

    prod = x * y * z;
    cout << "\nThe product is " << prod;

    if (x < y, x < z)
      {cout << "\nSmallest is " << x;}

    if (y < x, y < z)
      {cout << "\nSmallest is " << y;}

    if (z < x, z < y)
      {cout << "\nSmallest is " << z;}

    if (x > y, x > z)
      {cout << "\nLargest is " << x << endl;}

    if (y > x, y > z)
      {cout << "\nLargest is " << y << endl;}

    if (z > x, z > y)
      {cout << "\nLargest is " << z << endl;}

    return 0;
}

附:我这样做是为了学习,这不是家庭作业。

【问题讨论】:

  • &amp;&amp; 是逻辑与。 || 是逻辑或。

标签: c++ math pearson


【解决方案1】:

if条件需要重写

if (x < y, x < z)

成为

if (x < y && x < z)

如果你有条件,对所有剩余的都做同样的事情。

编辑: 所有用逗号分隔的表达式都将被评估,所以如果你有类似的东西 x = 5, y = 6; 它将评估它们并将 x 设置为 5 并将 y 设置为 6 但 z = (x=5, y=6); 这将导致 z 设置为 6,就像 y 一样,因为 y=6 是逗号分隔项列表中的最后一项。

【讨论】:

  • 非常感谢!我现在知道使用 &&。
  • 我添加了更多细节以帮助您了解为什么它不能使用逗号,请查看我为您编写的示例代码:codepad.org/sknsEk3i
【解决方案2】:
int main() {

  std::cout << "Enter three numbers: ";

  int sum = 0;
  double avg = 0.;
  int product = 0;
  int smallest = std::numeric_limits<int>::max();
  int largest = std::numeric_limits<int>::min(); // the initializers here might not be correct, but the gist is in place...

  for (int i = 0; i < 3; ++i) {
    int val = 0;
    std::cin >> val;

    sum += val;
    avg += val;
    product *= val;

    if (val < smallest) smallest = val;
    if (val > largest) largest = val;
  }
  avg /= 3.; // This can also be done in the for loop, I just forget how.

  std::cout << "Sum: " << sum;
  // etc...  The calculations are all done.
}

【讨论】:

    【解决方案3】:

    将逗号替换为 && 用于 AND 运算符,这意味着两个条件都必须为真,或者 ||这是一个 OR 运算符,如果您希望满足任何或两个条件。

    来自 C++ 文档:

    The comma operator (,) is used to separate two or more expressions that are included    
    where only one expression is expected. When the set of expressions has to be evaluated 
    for a value, only the rightmost expression is considered.
    

    【讨论】:

      【解决方案4】:

      你想要 && 而不是逗号 即

      if (x < y , x < z)
        {cout << "\nSmallest is " << x;}
      

      应该是

      if (x < y && x < z)
        {cout << "\nSmallest is " << x;}
      

      【讨论】:

        【解决方案5】:

        在 if 条件中使用 &amp;&amp; 代替 ,

        【讨论】:

          【解决方案6】:

          现在您意识到&amp;&amp; 用于AND,您应该使用此运算符而不是逗号,。但是您知道您还可以使用关键字and 代替其等效符号吗?:

          if ( x < y and x < z ) {
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-05-25
            • 2014-01-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多