【发布时间】:2012-08-01 10:40:23
【问题描述】:
我从 Stackoverflow.com 阅读了一些文章,尤其是:
What's the most concise way to get the inverse of a Java boolean value?
Easiest way to flip a boolean value?
如果我有三个布尔变量会发生什么?我想使用单行分配一个真/假值。
例如,test1 和 test3 必须为真,test2 必须为假。
我用过
test1 = test2 ^= test3 = true; //true, false, true
或
test1 = test3 ^= test2 ^= true;
但这并不好。逻辑不好。
我知道我的问题很简单,但我有 6-7 个布尔变量,如果可能的话,我想使用单行赋值。
有可能吗?
【问题讨论】:
-
虽然我很想知道这是否可能,但请考虑一下可读性:您到底为什么要做这样的事情?
-
“因为它只适用于条件语句或循环语句”是什么意思?你能发布一个这个限制的例子吗?
-
@Digitalex:我问是否可能。
-
为什么必须在一行上?你可以做
test1 = test3 = ... = true; test2 = test4 = ... = false;,这只有两行。 -
var test = new BitVector32(5);
标签: c# boolean boolean-logic