Generating truth tables in Java

2019-02-07 07:56发布

I'm trying to print some truth tables as part of a school assignment. How can I generate a dynamic size truth table in Java?

So that printTruthTable(1) prints:

0
1

printTruthTable(3) prints:

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

And so on. I have been trying to implement it using recursion, but I just can't get it right.

7条回答
smile是对你的礼貌
2楼-- · 2019-02-07 08:49

If you look at what you're generating, it appears to be counting in binary. You're going to be counting to 2^(n) - 1 in binary and spitting out the bits.

查看更多
登录 后发表回答