c++程序逻辑错误,求解!
以下程序,要求用户三个数字,然后打印最大的数字。程序存在逻辑错误,对某些输入数字,不能按预期。求解?
你漏掉了 n3 > n2 > n1 的情况,这种情况下,n3也是最大的。
else if (n2 > n3)
cout << n2 << " is the largest.";
修改为
else
{
if (n2 > n3)
cout << n2 << " is the largest.";
else
cout << n3 << " is the largest.";
}
其实直接写
int max = (n1 > n2) ? n1 : n2;
max = (max > n3) ? max : n3;
cout << max << " is the largest.";
即可。
#include
int main()
{
int max.a,b.c;
printf("请输入abc的值: \n");
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
max=(a>b)?a:b;
max=(max>c)?max:c;
return 0;
}