I
I
Ilya2017-09-23 11:37:54
Java
Ilya, 2017-09-23 11:37:54

How to display truth table in Java?

You need to write a logical expression in Java. Here is the task: https://yadi.sk/i/05km155F3NA8zK

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2017-10-05
@sidorchik

The code

public class Main {
 
    public static void main(String[] args) {
        System.out.println("R = !(!x && y) || (x && !z)");
        System.out.println("Таблица истинности");        
        System.out.println("X Y Z  R");
    out(false,false,false);
    out(false,false,true);
    out(false,true,false);
    out(false,true,true);
    out(true,false,false);
    out(true,false,true);
    out(true,true,false);
        out(true,true,true);
    }
    public static void out(boolean x, boolean y, boolean z){
        System.out.println((x ? "1 " : "0 ") + (y ? "1 " : "0 ") + (z ? "1 " : "0 ") + (!(!x && y) || (x && !z) ? " 1" : " 0"));
    }
}

R = !(!x && y) || (x && !z) XYZR
truth table 0 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 1

T
twobomb, 2017-09-23
@twobomb

I'll wash it somehow.

public class Main {

    public static void main(String[] args) {
        System.out.println("Таблица истиности для выражения !(!x && y) || (x && !z)");
        System.out.println("  X      Y      Z    !(!x && y) || (x && !z)");
        out(true,true,true);
        out(false,false,false);
        out(true,true,false);
        out(false,false,true);
        out(true,false,true);
        out(false,true,false);
        out(true,false,false);
        out(false,true,true);

    }
    public static void out(boolean x,boolean y,boolean z){
        System.out.println((x?"Истина ":" Ложь  ")+(y?"Истина ":" Ложь  ")+(z?"Истина ":" Ложь  ")+(!(!x && y) || (x && !z)?"         Истина":"         Ложь"));
    }
}

Truth table for expression !(!x && y) || (x && !z)
XYZ !(!x && y) || (x && !z)
True True True True
False False True
True True False True
False True True
True False True True
False True False False
True False True
False True False

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question