在Java中生成真值表

11
我正在尝试打印一些真值表来完成学校作业。如何在Java中生成一个动态大小的真值表?
因此,printTruthTable(1)应该打印:
0
1

printTruthTable(3)会打印:

0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

等等,我一直在尝试使用递归来实现它,但我就是做不对。

8个回答

18

这是我对你的问题的看法,用一个小类紧凑地编写了所有内容,只需复制/粘贴即可。

注意如何使用模运算符2(%符号)从循环索引中获取0和1。

public class TruthTable {
    private static void printTruthTable(int n) {
        int rows = (int) Math.pow(2,n);

        for (int i=0; i<rows; i++) {
            for (int j=n-1; j>=0; j--) {
                System.out.print((i/(int) Math.pow(2, j))%2 + " ");
            }
            System.out.println();
        }
    }
    public static void main(String[] args) {
        printTruthTable(3); //enter any natural int
    }
}

1
这很好。您还可以将对 Math.pow(2,n) 的调用替换为 (1 << n) - Kingsley

13

这不是一个真值表,而是一个二进制数字表格。您可以使用Java的Integer.toBinaryString方法生成所需的零和一;插入空格应该很简单。

int n = 3;
for (int i = 0 ; i != (1<<n) ; i++) {
    String s = Integer.toBinaryString(i);
    while (s.length() != 3) {
        s = '0'+s;
    }
    System.out.println(s);
}

为了使其动态化,您应该将s.length() != 3替换为s.length() != n - finebel

2
递归的魔力:
public static void main(String args[]) {
    int size = 3;
    generateTable(0, size, new int[size]);
}

private static void generateTable(int index, int size, int[] current) {
    if(index == size) { // generated a full "solution"
        for(int i = 0; i < size; i++) {
            System.out.print(current[i] + " ");
        }
        System.out.println();
    } else {
        for(int i = 0; i < 2; i++) {
            current[index] = i;
            generateTable(index + 1, size, current);
        }
    }
}

1

如果你看一下你所生成的内容,它似乎是在二进制中计数。你将会以二进制计数到2^(n) - 1,并输出比特位。


0

the truth table is base on the binary representation of the number but without removing leading zero's so what you would do is to loop from 0 to (1<

public void  generate(int n){
    for (int i=0 ;i!=(1<<n);i++) {
        String binaryRep = Integer.toBinaryString(i);
        while (s.length() != n) {
            binaryRep = '0'+binaryRep;
        }
        System.out.println(s);
    }
}

you can make that using recursion also :

public void generateRecursively(int i , int n){
    if(i==(1<<n))
        return;
    else{
        String temp = Integer.toBinaryString(i);
        while(temp.length()<n){
            temp = '0'+temp;
        }
        System.out.println(temp);
        generateRecursively(i+1,n);
    }
}


在非递归函数中,应该是'while (binaryRep.length()...',以及'System.out.println(binaryRep);'(使用binaryRep而不是s)。 - Tony

0

对于你的问题,我有更详细的解释。

import java.util.Scanner;
    public class tt{
        boolean arr[][];
        boolean b=false;
        boolean[][] printtt(int n){
            for(int i=0;i<n;i++){
                for(int j=0;j<(Math.pow(2,n));j++){

                        if(j<Math.pow(2,n-1)){
                            arr[j][i]=b;
                        }
                        else{
                            arr[j][i]=!b;
                        }
                }
                }
                return(arr);
            }


        public static void main(String args[]){
            Scanner sc=new Scanner(System.in);
            System.out.println("Input values count");
            tt ob=new tt();
            int num=sc.nextInt();int pownum=(int)Math.pow(2,num);
            boolean array[][]=new boolean[pownum][num];
            array=ob.printtt(num);
            for(int i=0;i<num;i++){
            for(int j=0;j<(Math.pow(2,num));j++){

                    System.out.println(array[j][i]);
                }
        }
    }
    }

0
最近我也遇到了类似的问题,只不过我的项目是为给定的逻辑表达式生成真值表。以下是我为独立变量分配真值所想出的方法。
    column = 0;

    while (column < numVariables)
    {
        state = false;
        toggle = (short) Math.pow(2, numVariables - column - 1);

        row = 1;
        while (row < rows)
        {
            if ((row -1)%toggle == 0)
                state = !state;

            if (state)
                truthTable[row][column] = 'T';
            else
                truthTable[row][column] = 'F';

            row++;
        }

        column++;
    }

这是假设您的第一行填充了变量名和子表达式。如果您想从第0行开始,则数学可能会略有变化。
这段代码...
if ((row -1)%toggle == 0)
将变成...
if (row%toggle == 0)

0
这个简单的程序将您给定数量输入的真值表存储在一个int数组中并打印出来。
import java.util.Scanner;

public class Main{

public static class TruthTable {
    public static int rows;
    public static int nodes;
    public static int[][] tt = new int[0][0];


    TruthTable(int n) {
        this.nodes = n;
        this.rows = (int) Math.pow(2,n);
        tt = new int[rows][nodes];

        for (int i=0; i<rows; i++) {
            for (int j=n-1; j>=0; j--) {
                tt[i][j] = (i/(int) Math.pow(2, j))%2;
            }  
        }
    }   

    void printTable(){
        for (int i=0; i<rows; i++) {
            for (int j=nodes-1; j>=0; j--) {
                System.out.printf("%d ", tt[i][j]);
            }
            System.out.println();

        }

    }
}
public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);
    System.out.println("Enter Size of Population: ");
    int numberOfNodes = myObj.nextInt();
    TruthTable myTable = new TruthTable(numberOfNodes);
    //TruthTable.printTruthTable(3);
    System.out.println();

    myTable.printTable();

}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接