#include <iostream>
using namespace std;
int main()
{
int x = 0;
int y = 0;
int input = 0;
cout << "Input a number:" << endl;
cin >> input;
input ? ++x, ++y : --x, --y;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
return 0;
}
如上代码输入1的话输出x=1,y=0
输入0的话输出x=-1,y=-1
奇怪的是把x和y交换位置输出结果一样,仍然是输入1输出x=1,y=0,输入0输出x=-1,y=-1
#include <iostream>
using namespace std;
int main()
{
int x = 0;
int y = 0;
int input = 0;
cout << "Input a number:" << endl;
cin >> input;
input ? ++y, ++x : --x, --y;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
return 0;
}
++y, ++x
和++x, ++y
是一样的啊,它们中间是,
,不是;
,属于一个语句,没毛病啊。