【发布时间】:2012-09-14 20:57:30
【问题描述】:
我试图为如何编程中的练习 2.19 编写一个程序,但遇到了一些困难。
程序应该让用户输入三个整数,然后显示这些整数中的sum、average 和product。
我遇到的唯一问题是显示最大和最小。当我运行程序并输入三个整数(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;
}
附:我这样做是为了学习,这不是家庭作业。
【问题讨论】:
-
&&是逻辑与。||是逻辑或。