Question


   J'ai un programme java où je veux valider si l'un de 3 booléens est faux. Je veux trouver la plus petite expression que je peux écrire pour vérifier contre les permutations.

if(!(needsWork && (needsApproval || isAdmin) ))

Je pense que cela est suffisant pour faire en sorte que si l'un des 3 booléens est faux, je veux arrêter le traitement. Cependant, je le soupçon faufilant je manque quelque chose.

Était-ce utile?

La solution

Would if (!needsWork || !needsApproval || !isAdmin) fonctionne pas? Java prend en charge l'évaluation de court-circuit.

Autres conseils

Depuis

`any 3 booleans are false` (i.e. `!a || !b || !c`)

et

`(! (needsWork && (needsApproval || isAdmin))` (i.e. (! (a && ( b || c))`

ont différentes tables de vérité , êtes-vous sûr que les cas qui diffèrent importent peu?

a b c   (!a || !b || !c)    (! (a && (b || c)))
T T T          F                    F          
T T F          T                    F
T F T          T                    F
T F F          T                    T
F T T          T                    T
F T F          T                    T
F F T          T                    T
F F F          T                    T

Transformations

Je vais souvent jouer avec des expressions booléennes pour essayer de clarifier ou de les simplifier et j'utiliser ces transformations logiques pour me aider:

// You can push (distribute) `!` into parenthesis if you reverse the `||` or `&&` operator inside:
! (a || b)             <=> (! a && ! b)
! (a || b || c || ...) <=> (! a && ! b && ! c && ...)

! (a && b)             <=> (! a || ! b)
! (a && b && c && ...) <=> (! a || ! b || ! c || ...)

// You can drop parens when the boolean operator outside the parens is the same as inside:
(a || (b || c || ...)) <=> (a || b || c)
(a && (b && c && ...)) <=> (a && b && c)

// You can push (distribute) a boolean op into parenthesis by applying it to each term inside:
(a || (b && c)         <=> ((a || b) && (a || c)
(a || (b && c && ...)  <=> ((a || b) && (a || c) && (a || ...) ...

(a && (b || c)         <=> ((a && b) || (a && c))
(a && (b || c || ...)  <=> ((a && b) || (a && c) || (a || ...) ...

// XOR means the term values have to be different:
(a ^ b)                <=> ((a && !b) || (!a && b))

// XOR means the same as OR unless both terms are true:
(a ^ b)                <=> ((a || b) && ! (a && b))

Il y a bien sûr beaucoup d'autres, mais ce sont ceux que j'utilise le plus souvent. Cela peut sembler compliqué, mais ils sont faciles à apprendre à connaître par cœur dès que vous commencez par les pratiquer.

Dans votre cas, si vous voulez voir ce que certaines déclarations équivalentes possibles pour:

(! (needsWork && (needsApproval || isAdmin) ))

voici quelques transformations:

(! (needsWork && (needsApproval || isAdmin) ))   => [push the '!' through the `()`]
(! needsWork || ! (needsApproval || isAdmin) )   => [push the 2nd '!' through the `()`]
(! needsWork || (! needsApproval && ! isAdmin))

mais je ne vois pas de véritable simplification de ce que vous avez.

Bien sûr, si vérifier que any of 3 booleans are false est très bien, vos choix sont simples

(! needsWork || ! needsApproval || ! isAdmin) => [or pull the `!` outside the `()`]
(! (needsWork  && needsApproval && isAdmin))
if(!(needsWork & needsApproval & isAdmin))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top