//三元運算子 條件 ? ture : false
當條件成立的時候 ?(true) 後面回傳你要做的事, false) 回傳條件不成立時候你要做的事- #include<cstdlib>
- #include<iostream>
- using namespace std;
- int main()
- {
- int num1,num2,num3;
- int max,min,mid;
- cout << "第一個數";
- cin >> num1;
- cout << "第二個數";
- cin >> num2;
- cout << "第三個數";
- cin >> num3;
-
- max = num1;
- //三元運算子 ? ture : false
- max = num2>max?num2:max;
- max = num3>max?num3:max;
-
- min = num1;
- min = num2 < min ? num2:min;
- min = num3 < min ? num3:min;
-
- mid = num1;
- mid = (mid == max) || (mid==min) ? num2:mid;
- mid = (mid == max) || (mid==min) ? num3:mid;
-
- cout << max << endl;
- cout << mid << endl;
- cout << min << endl;
-
- system("pause");
- return 0;
- }
複製代碼 |