显示最大和最小数字时出错

use*_*432 0 c++ math pearson

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

该方案应该让用户输入三个整数,然后显示sum,averageproduct那些整数.

我唯一的问题是显示最大和最小.当我运行程序并输入三个整数时(8, 9, and 10),输出读取Smallest is 8 AND Smallest 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;
}
Run Code Online (Sandbox Code Playgroud)

PS我这样做是为了学习,这不是功课.

Mah*_*yez 7

如果条件,你需要重写这个

if (x < y, x < z)
Run Code Online (Sandbox Code Playgroud)

成为

if (x < y && x < z)
Run Code Online (Sandbox Code Playgroud)

如果你有条件,那么对剩余的所有条件也一样.

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