问题
I have a question for an assignment. The questions is basically the following: There are 2 integer variables say A and B. Both these integers contain data. Using a truth table, which, if any, of the following IF statement tests is equivalent to:
if (!(A == 60 && B == 40))
- if (A != 60 || B != 40)
- if (A == 60 && (!(B == 40)))
How would i tackle this please. Anything advice would be appreciated. I think that I have to create a table with three columns - one called A, another B, and the third column called RESULT (YES OR NO).
The statement: if (!(A == 60 && B == 40))
- I am not to sure how to read the part if (!
. In other words, the part A == 60 && B == 40
is telling me essentially that A must equal 60 AND AT THE SAME TIME B must equal 40. Following that I am confused. Any help/advise please would be appreciated.
Thanks Chris
回答1:
This really has nothing to do with Java per se. Yes, you can solve by writing the truth tables. The !
means logical negation or not or you may even think of it as opposite. Personally, I find it helpful to establish all the parts of a particular truth table.
a | b | !b | a & b | a & !b | !(a & b)
----------------------------------------------------------------------------------------
A = 60 | B = 40 | !(B = 40) | (A = 60 & B = 40) | A = 60 & !(B = 40) | !(A = 60 & B = 40)
T | T | F | T | F | F
T | F | T | F | T | T
F | T | F | F | F | T
F | F | T | F | F | T
You should note your particular example is subject to one of De Morgan's Laws.
P is A = 60
Q is B = 40
¬ is !
∧ is &&
∨ is ||
so...
!(A && B)
is really the same as !A || !B
The truth table tells you the rest you need to know to solve that problem.
回答2:
I hope this code that I create help you to get an idea, this is a simple code to display the Truth Table using binary (0s and 1s) numbers instead of Boolean (TRUE, FALSE)
/**
* Truth table for the logical operators. Using
* zeros and ones.
*
* @ Samuel Mayol
*/
public class LogicalOpTable {
public static void main(String[] args) {
boolean p, q;
byte p1, q1, pAndQ, pOrQ, pXORq, notP, pq;
System.out.println("Using 0s and 1s for the Truth Table:");
System.out.println("P\tQ\tAND\tOR\tXOR\tNOT"); // using tabs \t
pq =1;
switch (pq) { // without break it will execute the whole swich case statements
case 1 :
p = true; q = true;
p1=(byte) (p?1:0); // The ? : operator in Java
q1=(byte) (q?1:0); // Short form for Java If statement
pAndQ=(byte) ((p&q)?1:0);
pOrQ=(byte) ((p|q)?1:0);
pXORq=(byte) ((p^q)?1:0);
notP= (byte) ((!p)?1:0);
System.out.print(p1 + "\t" + q1 + "\t");
System.out.print(pAndQ + "\t" + pOrQ + "\t");
System.out.println(pXORq + "\t" + notP);
case 2:
p = true; q = false;
p1=(byte) (p?1:0); // The ? : operator in Java
q1=(byte) (q?1:0); // Short form for Java If statement
pAndQ=(byte) ((p&q)?1:0);
pOrQ=(byte) ((p|q)?1:0);
pXORq=(byte) ((p^q)?1:0);
notP= (byte) ((!p)?1:0);
System.out.print(p1 + "\t" + q1 + "\t");
System.out.print(pAndQ + "\t" + pOrQ + "\t");
System.out.println(pXORq + "\t" + notP);
case 3:
p = false; q = true;
p1=(byte) (p?1:0); // The ? : operator in Java
q1=(byte) (q?1:0); // Short form for Java If statement
pAndQ=(byte) ((p&q)?1:0);
pOrQ=(byte) ((p|q)?1:0);
pXORq=(byte) ((p^q)?1:0);
notP= (byte) ((!p)?1:0);
System.out.print(p1 + "\t" + q1 + "\t");
System.out.print(pAndQ + "\t" + pOrQ + "\t");
System.out.println(pXORq + "\t" + notP);
case 4:
p = false; q = false;
p1=(byte) (p?1:0); // The ? : operator in Java
q1=(byte) (q?1:0); // Short form for Java If statement
pAndQ=(byte) ((p&q)?1:0);
pOrQ=(byte) ((p|q)?1:0);
pXORq=(byte) ((p^q)?1:0);
notP= (byte) ((!p)?1:0);
System.out.print(p1 + "\t" + q1 + "\t");
System.out.print(pAndQ + "\t" + pOrQ + "\t");
System.out.println(pXORq + "\t" + notP);
}
}
}
The result after running this code is:
Using 0s and 1s for the Truth Table:
P Q AND OR XOR NOT
1 1 1 1 0 0
1 0 0 1 1 0
0 1 0 1 1 1
0 0 0 0 0 1
来源:https://stackoverflow.com/questions/36369288/truth-table-assistance