Truth Table all possible combinations

huangapple go评论76阅读模式
英文:

Truth Table all possible combinations

问题

public class truthTables {

  public static void main(String[] args) {

    boolean t = true;
    boolean f = false;

    boolean[] p = {f, f, f, f, t, t, t, t};
    boolean[] q = {f, f, t, t, f, f, f, f};
    boolean[] r = {f, t, f, t, f, t, f, t};
    boolean[] proposition = new boolean[8];
    int tableNumber = 1;

    while (tableNumber <= 256) {

      System.out.println("Table " + tableNumber + " ");
      System.out.println("P  q  r  proposition ");
      System.out.println("________________");

      int i = 0;

      while (i <= 7) {

        if (proposition[i] == true) {
          System.out.println(p[i] + "  " + q[i] + "  " + r[i] + " " + proposition[i]);
        }

        if (proposition[i] == false) {
          System.out.println(p[i] + "  " + q[i] + "  " + r[i] + " " + proposition[i]);
        }

        i = i + 1;
      }

      tableNumber = tableNumber + 1;
    }
  }
}
英文:

I am currently working on an assignment for school but I'm lost when it comes to implementing into code.

The idea of the assignment is to create multiple True and False tables of (p, q, r, Proposition). The values p, q, r, can be hard coded in meaning I can just create an array with the values and I do not have to worry about those. But when it comes to the last array proposition I have to create all possible combinations out of an array length 8 and print them out.

The thing is I been searching and I can't find something related to this. I'm not asking to do the assignment for me but I want know if someone can explain to me how is it done. This is so far the code I have made it's not much but you can actually tell I'm stuck, also when I asked they told me it's possible to do it in 8 nested loops so I'm currently working on this feedback is appreciated. Thanks

public class truthTables {
public static void main(String[] args) {
boolean t = true;
boolean f = false;
boolean[] p = {f, f, f, f, t, t, t, t};
boolean[] q = {f, f, t, t, f, f, f, f};
boolean[] r = {f, t, f, t, f, t, f, t};
boolean[] proposition = new boolean[8];
int tableNumber = 1;
while (tableNumber &lt;= 256) {
System.out.println(&quot;Table &quot; + tableNumber + &quot; &quot;);
System.out.println(&quot;P &quot; + &quot; q &quot; + &quot; r &quot; + &quot; proposition &quot;);
System.out.println(&quot;________________&quot;);
int i = 0;
while (i &lt;= 7) {
i = i;
if (proposition[i] = true) {
System.out.println(p[i] + &quot;  &quot; + q[i] + &quot;  &quot; + r[i] + &quot; &quot; + proposition[i]);
}
if (proposition[i] = false) {
System.out.println(p[i] + &quot;  &quot; + q[i] + &quot;  &quot; + r[i] + &quot; &quot; + proposition[i]);
}
i = i + 1;
}
tableNumber = tableNumber + 1;
}
}
}

答案1

得分: 2

需要创建一个数组长度为8的所有可能组合。

您可以通过检查表号的位来创建第`tableNumber`个表:

    int b = 0;            // “位索引”,您将要设置命题的元素。
    int t = tableNumber;  // 可以更新的tableNumber的副本。

    while (t != 0) {
      proposition[b] = (t & 1) != 0;  // 最低有效位是1吗?

      b++;       // 增加位索引。
      t >>= 1;  // 右移1位,去掉最低有效位。
    }

您也可以将其写成一个for循环

    for (int b = 0, t = tableNumber; t != 0; t >>= 1, b++) {
      proposition[b] = (t & 1) != 0;
    }
英文:

> I have to create all possible combinations out of an array length 8

You can create the tableNumber-th table by checking its bits:

int b = 0;            // &quot;Bit index&quot;, the element of proposition you are going to set.
int t = tableNumber;  // Copy of tableNumber that you can update.
while (t != 0) {
proposition[b] = (t &amp; 1) != 0;  // Is the least significant bit 1?
b++;       // Increase the bit index.
t &gt;&gt;&gt;= 1;  // Right-shift by 1, chopping off the least significant bit.
}

You can write this as a for loop too:

for (int b = 0, t = tableNumber; t != 0; t &gt;&gt;&gt;=1, b++) {
proposition[b] = (t &amp; 1) != 0;
}

答案2

得分: 0

我认为你卡住的第一部分是你没有用任何新内容编辑你的 proposition 数组。所以在你的 while 循环内,你需要有这部分代码:

for (j = 0; j < 8; j++) {
    if (((tableNumber >> i) & 1) == 1) {
        proposition[i] = t;
    } else {
        proposition[i] = f;
    }
}

这段代码是我从这个帖子中得到的。它会给你一个数组,其中包含了:

table 0: f, f, f, f, f, f, f, f
table 1: f, f, f, f, f, f, f, t
table 2: f, f, f, f, f, f, t, f
table 3: f, f, f, f, f, f, t, t
.
.
.
table 255: t, t, t, t, t, t, t, t

然后你需要让一个表格编号从 0 开始,然后你可以像这样显示它们:

public class truthTables {
  
  public static void main(String[] args) {

    boolean t = true;
    boolean f = false;

    boolean[] p = {f, f, f, f, t, t, t, t};
    boolean[] q = {f, f, t, t, f, f, f, f};
    boolean[] r = {f, t, f, t, f, t, f, t};
    boolean[] proposition = new boolean[8];
    int tableNumber = 0; // start at 0

    while (tableNumber < 256) {

      System.out.println("Table " + (tableNumber + 1) + " ");
      System.out.println("P " + " q " + " r " + " proposition");
      System.out.println("________________");

     /** add code here **/
      for (j = 0; j < 8; j++) {
        if (((tableNumber >> j) & 1) == 1) {
            proposition[j] = t;
        } else {
            proposition[j] = f;
        }
      }
      /** to here **/

      int i = 0;

      while (i <= 7) {
        System.out.println(p[i] + "  " + q[i] + "  " + r[i] + " " + proposition[i]);

        i = i + 1;
      }

      tableNumber = tableNumber + 1;
    }
  }
}

基本上你正在做的是将表格编号拆分为其位组件,从 0 到 255,即位表示为 00000000 到 11111111,这是你要寻找的每个表格组合。还有其他的编码方式可以节省空间,但我尝试遵循你的编码风格。

**编辑:**其中一种方法是完全避免使用 for 循环,在你的 while 循环中计算正确的值。

int i = 0;

while (i <= 7) {
    boolean propositionBool = ((tableNumber >> i) & 1) == 1;
    System.out.println(p[i] + "  " + q[i] + "  " + r[i] + " " + propositionBool);

    i = i + 1;
}
英文:

I think the first part that you are getting stuck on is the fact that you aren't editing your proposition array with anything new. So within your while loop you need to have this

for (j = 0; j &lt; 8; j++) {
if (((tableNumber &gt;&gt; i) &amp; 1) == 1) {
proposition[i] = t;
} else {
proposition[i] = f;
}
}

which I got from this post. It will give you an array of

table 0: f, f, f, f, f, f, f, f
table 1: f, f, f, f, f, f, f, t
table 2: f, f, f, f, f, f, t, f
table 3: f, f, f, f, f, f, t, t
.
.
.
table 255: t, t, t, t, t, t, t, t

Then you have to have a table number start at 0, then you can display them as this

public class truthTables {
public static void main(String[] args) {
boolean t = true;
boolean f = false;
boolean[] p = {f, f, f, f, t, t, t, t};
boolean[] q = {f, f, t, t, f, f, f, f};
boolean[] r = {f, t, f, t, f, t, f, t};
boolean[] proposition = new boolean[8];
int tableNumber = 0; // start at 0
while (tableNumber &lt; 256) {
System.out.println(&quot;Table &quot; + (tableNumber + 1) + &quot; &quot;);
System.out.println(&quot;P &quot; + &quot; q &quot; + &quot; r &quot; + &quot; proposition&quot;);
System.out.println(&quot;________________&quot;);
/** add code here **/
for (j = 0; j &lt; 8; j++) {
if (((tableNumber &gt;&gt; j) &amp; 1) == 1) {
proposition[j] = t;
} else {
proposition[j] = f;
}
}
/** to here **/
int i = 0;
while (i &lt;= 7) {
System.out.println(p[i] + &quot;  &quot; + q[i] + &quot;  &quot; + r[i] + &quot; &quot; + proposition[i]);
i = i + 1;
}
tableNumber = tableNumber + 1;
}
}
}

Basically what you are doing is taking the table number, breaking it down into its bit components of 0 - 255 which in bits is 00000000 - 11111111 which is every table combination you are looking for. There are other ways you can code this that will save space, but I tried following your coding style.

EDIT: one of which is avoid the for loop all together and calculating the correct value in your while loop.

      int i = 0;
while (i &lt;= 7) {
boolean propositionBool = ((tableNumber &gt;&gt; i) &amp; 1) == 1;
System.out.println(p[i] + &quot;  &quot; + q[i] + &quot;  &quot; + r[i] + &quot; &quot; + propositionBool);
i = i + 1;
}

huangapple
  • 本文由 发表于 2020年8月27日 02:51:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63603951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定