Hi my question is in the header, i really tried to find how to do this but couldn't find any solution yet, im a newbie so i really need your help guys.
bool a;
bool b;
bool c;
Simply : a,b,c are false : make one true then make false again when others remains false, and make one true again.
There are 3 options: A - B - C, and i want to do 3 things with if statements,
if(a) ....
if(b)....
if(c) ....
so, everytime i need to choose(randomly) only one of them (a or b or c),
CodePudding user response:
From the recent comments, it sounds like what you're actually trying to do is choose randomly between three options. Forget about bools; what you need is simply:
var rand = new Random(); // ideally keep this between uses - maybe a field
//...
switch (rand.Next(3)) // means 0-2
{
case 0: // a
//...
break;
case 1: // b
//...
break;
case 2: // c
//...
break;
}
CodePudding user response:
It is a little unclear what you're asking. I would start by describing the state's you want to represent. If it is "none, a-only, b-only, c-only", then I wouldn't bother with the bools - I'd just use a counter from 0-3, and work from there. You could also use a [Flags] enum
with the values 0
, 0b001
, 0b010
and 0b100
but IMO that is over-complicating things. Another option (if that is unclear) could be an array of tuples of the states you want, for example
new[] {(false,false,true),(false,true,false),(true,false,false)}
This could also be done by using the loop counter as a "left-shift" operand from the literal 1
, and using binary arithmetic.
If the states are "all combinations of on/off pairs over 3 bools", then that's just binary integer arithmetic - you'd do a loop from 0-7 and look at the bitwise components of the loop iterator.
If none of this makes sense: maybe try clarifying the question so we can see exactly what you're trying to do - then we can respond with code.