我从Stackoverflow.com上读了一些文章,特别是:
What's the most concise way to get the inverse of a Java boolean value?
Easiest way to flip a boolean value?
如果我有三个布尔变量,会发生什么?我想使用单行赋值为true/false。
例如,test1和test3必须为true,而test2必须为false。
我用过
test1 = test2 ^= test3 = true; //true, false, true或
test1 = test3 ^= test2 ^= true;但这并不好。逻辑不是很好。
我知道我的问题很简单,但我有6-7个布尔变量,如果可能的话,我想用单行赋值。
有可能吗?
发布于 2012-08-03 20:41:57
a = b = c = !(d = e = f = true);这会将第一个设置为false,将第二个设置为true。(从D开始。)
所以在你的例子中:
test2 = !(test1 = test3 = true);发布于 2012-08-03 19:26:57
您可以这样做:
test1 = !(test2 = !(test3 = true));尽管您绝对应该将赋值拆分为多行:
test1 = true;
test2 = false;
test3 = true;我想不出在c#中有什么地方可以做前者,但不能做后者。
https://stackoverflow.com/questions/11794748
复制相似问题