我正在尝试打印一些真值表作为学校作业的一部分。如何在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
等等。我一直在尝试使用递归来实现它,但我无法正确使用它。
答案 0 :(得分:14)
这是我对你的问题的看法,所有写得很好而且很小,只需复制/粘贴
注意我是如何使用modulo2(%符号)从循环索引中获取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 :(得分:11)
这不是真值表 - 而是一个二进制数表。您可以使用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);
}
答案 2 :(得分: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);
}
}
}
答案 3 :(得分:1)
如果你看看你正在生成什么,它似乎是以二进制计数。你将以二进制计数到2 ^(n) - 1并吐出这些位。
答案 4 :(得分:0)
真值表基于数字的二进制表示,但不删除前导零 所以你要做的是从0循环到(1&lt;
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);
}
}
你也可以使用递归来实现:
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);
}
}
答案 5 :(得分: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]);
}
}
}
}
答案 6 :(得分: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)
答案 7 :(得分: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();
}
}