This discussion is archived
2 Replies Latest reply: Jul 15, 2010 12:52 PM by 747719 RSS

Truth Table Simulation Algorithm

747719 Newbie
Currently Being Moderated
Hi All

I wish to write a method that takes a number and prints out the appropriate truth table values
i.e. No = 2

00
01
10
11
or No = 3

000
001
010
011
100
101
110
111
public class MyTruthTable
{

         public static void main (String args [])
            { 
                 int numb = 10;

              for(i=0; i<numb; i++)
                {
                  int currentNumb = i;
                  
                         for(j=0; j<numb; j++)
                             {
                                  if(currentNumb.equals(anotherNumb)){}
                                  else
                                    {
                                       //Mind Blank
                                      System.out.println("   ");
                                    }


                }

}
}
Thanks
  • 1. Re: Truth Table Simulation Algorithm
    800282 Newbie
    Currently Being Moderated
    Here you go:
    public class Test {
         
      public static void printTruthTable(int n) {
        for(int i = 0; i < Math.pow(2, n); i++) {
          System.out.println(String.format("%"+n+"s", Integer.toBinaryString(i)).replace(' ', '0'));
        }
      }
         
      public static void main(String[] args) {
        printTruthTable(3);
      }
    }
    
    /*
        000
        001
        010
        011
        100
        101
        110
        111
    */
  • 2. Re: Truth Table Simulation Algorithm
    747719 Newbie
    Currently Being Moderated
    Hi there, thanks for your reply

    I have a slightly different but similiar question.

    I want to take in argument; an ArrayList of type String, and print out a kind of truth table in that order
    i.e.
    
    ArrayList <String> = {one, two, three}
    
    System prints out:
          one, -two, -three
          one,  two, -three
          one,  two,  three
    
          two,  -one, -three
          two,  one,  -three
          two,  one,   three
    
          three, -one,  -two
          three, one,   -two
          three, one,   two   
    Hope that made sense