请问为什么这段代码输出的结果永远是“小猪C”最重?


#include<iostream>
using namespace std;
int main() {
    int weight1 = 0;
    cout << "小猪A的体重为:" << endl;
    cin >> weight1;
    int weight2 = 0;
    cout << "小猪B的体重为:" << endl;
    cin >> weight2;
    int weight3 = 0;
    cout << "小猪C的体重为:" << endl;
    cin >> weight3;
    if (weight1 > weight2 > weight3 && weight1 > weight3 > weight2)
    {
        cout << "小猪A最重" << endl;
    }
    else if (weight2 > weight1 > weight3 && weight2 > weight3 > weight1)
    {
        cout << "小猪B最重" << endl;
    }
    else
    {
        cout << "小猪C最重" << endl;
        
    }
    system("pause");
    return 0;
}

if (weight1 > weight2 > weight3 && weight1 > weight3 > weight2)
这个表达式有问题,不能像数学一样连着写,应该要分开写
if ((weight1 > weight2 && weight2 > weight3) && (weight1 > weight3 && weight3 > weight2))

改一下吧:

 int getmax(int a,int b)
{
    if(a>b)
      return a;
    return b;
}
#include<iostream>
using namespace std;
int main() {
    int weight1 = 0;
    cout << "小猪A的体重为:" << endl;
    cin >> weight1;
    int weight2 = 0;
    cout << "小猪B的体重为:" << endl;
    cin >> weight2;
    int weight3 = 0;
    cout << "小猪C的体重为:" << endl;
    cin >> weight3;
    int max = getmax(weight1,getmax(weight2,weight3));
    if (weight1 == max)
    {
        cout << "小猪A最重" << endl;
    }
    else if (weight2 == max)
    {
        cout << "小猪B最重" << endl;
    }
    else
    {
        cout << "小猪C最重" << endl;
    }
    system("pause");
    return 0;
}