C++中條件操作符巢狀

weixin_34194087發表於2016-10-18
#include <iostream>
using namespace std;
int main()
{
    int i = 10, j = 20, k = 30;
    cout << "The larger value of "<< i << " and " << j << " is "<< ( i > j ? i : j ) << endl;
    cout << "The value of " << i << " is"<< ( i % 2 ? " odd." : " even." )<< endl;
    int max=i>j ?i>k?i:k:j>k?j:k;
    cout << "The larger value of "<< i << ", " << j << " and " << k<< " is " << max << endl;
}

編譯並執行這個程式產生下列輸出
The larger value of 10 and 20 is 20
The value of 10 is even
The larger value of 10, 20 and 30 is 3

相關文章