当我调用TruthTable类并使用输入填充它时,尝试设置AND门的输入时无法访问数组中的各个插槽?

threeAndGates.java-发生错误的类

import java.util.Scanner;

public class threeAndGates {

   public static void main(String[] args){
      LogicGate and1 = new LogicGate(LogicGate.AND);
      LogicGate and2 = new LogicGate(LogicGate.AND);
      LogicGate and3 = new LogicGate(LogicGate.AND);

      System.out.print("What is the number of Inputs? ");
      Scanner scan = new Scanner(System.in);
      int numOfInputs = scan.nextInt();

      System.out.print("What is the number of Outputs? ");
      int numOfOutputs = scan.nextInt();

      TruthTable Table1 = new TruthTable(numOfInputs,numOfOutputs);
      Table1.PopulateTruthTable();

      //below is where it is giving me "the type of the expression must be an array type but it resolves to TruthTable"
      for(int r = 0; r<(Math.pow(2, numOfInputs)) ; r++ ){
         and1.setInput1(Table1[r][0]);
         and1.setInput2(Table1[r][1]);
         and2.setInput1(Truth1[r][2]);
         and2.setInput2(Truth1[r][3]);
         and3.setInput1(and1.getOutput());
         and3.setInput2(and2.getOutput());

         Table1[r][numOfInputs + numOfOutputs] = and3.getOutput();
      }

      Table1.printTruthTable();
   }
}


TruthTable.java

public class TruthTable {
   private int numOfInputs;
   private boolean[][] table;

   public TruthTable(int inputs, int outputs){
      this.numOfInputs = inputs;
      int rows = (int) Math.pow(2,inputs);
      int columns =  inputs + outputs;
      table = new boolean[rows][columns];
   }

   public void printTruthTable(){
      for(int r = 0 ; r < table.length ; r++){
         for(int c = 0; c < table[r].length; c++)
            System.out.printf("%-5b ", table[r][c]);
         System.out.println();
      }
   }

   public String toString(){
      String outStr = new String();
      for(int r = 0; r < table.length; r++){
         for(int c = 0; c < table[r].length; c++)
            outStr += String.format("%-5b ", table[r][c]);
         outStr += '\n';
      }

      return outStr;
   }

   public boolean[][] PopulateTruthTable(){
      String s;
      String r ="";
      int[] Line = new int[numOfInputs];
      boolean bit;

      for ( int i= 0; i < Math.pow(2,numOfInputs) ; i++){
         int x = numOfInputs - Integer.toBinaryString(i).length();
         for(int j = 0; j<x ; j++)
            r += "0";
         s = r + Integer.toBinaryString(i);
         for(int k=0; k<s.length() ;k++){
           Line[k] = s.charAt(k)-48;
         }

         for(int m=0 ; m<numOfInputs ; m++){
           if(Line[m]==1) bit = true;
           else bit = false;
           table[i][m] = bit;
         }
         r="";
      }
      return table;
   }
}

最佳答案

您的TruthTable类不是数组。它包含一个数组。您可以在TruthTable类中添加get和set方法:

public boolean getValueAt(int x, int y) {
   return this.table[x][y];
}

public void setValueAt(int x, int y, boolean value) {
   this.table[x][y] = value;
}


并将其用于TruthTable值。



这与您的问题无关,但是在类中命名变量时,通常的做法是使用小写字母。例如,您有:

TruthTable Table1 = new TruthTable(numOfInputs,numOfOutputs);


会更好

TruthTable table1 = new TruthTable(numOfInputs,numOfOutputs);


可能最好

TruthTable truthTable = new TruthTable(numOfInputs,numOfOutputs);


您给事物命名的越好和越一致,便越容易读懂。

09-26 01:27