如何进行更改以使答案显示在多列而不是单列中?

时间:2018-06-06 04:31:51

标签: java arrays display

这是我编写的用于打印某些运算符输出的代码。但答案的显示并不是我需要的方式。

import java.io.*;
import java.util.*;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

// Java program to print all permutations of a
// given string.
public class Permutation
{
    public static void main(String[] args)
    {

        System.out.println("An incremented value");
        for(int i=2;i<=2;i++) {
            String p="";
            for(int j=0;j<=i;j++) {
                for(int m=0;m<j;m++) {
                    p=p+"&";
                        }
                for(int m=0;m<i-j;m++) {
                    p=p+"|";
                        }
                 printAllPermutations(p);
                p="";
            }
        }
    }

    static int factorial(int n) {
      int f = 1;
      for (int i = 1; i <= n; i++)
        f = f * i;
      return f;
    }

    static void print(char[] temp) {
        String a="";
      for (int i = 0; i < temp.length; i++)
      { System.out.print(temp[i]);
            a=a+temp[i];}

      System.out.println("operators:  "+temp.length);
      final int N = temp.length+1;  
      for (int i = 0; i < (1 << N); i++) {                          
        // System.out.println(zeroPad(Integer.toBinaryString(i), N));
         String b=zeroPad(Integer.toBinaryString(i), N)+"";
        // System.out.println("a:  "+a+"  b:"+b);
         char[] arrayA = b.toCharArray();
         char[] arrayB = a.toCharArray();
         StringBuilder sb = new StringBuilder();

         int ii = 0;
         while( ii < arrayA.length && ii < arrayB.length){
             sb.append(arrayA[ii]).append(arrayB[ii]);
             ++ii;
         }

         for(int j = ii; j < arrayA.length; ++j){
             sb.append(arrayA[j]);
         }

         for(int j = ii; j < arrayB.length; ++j){
             sb.append(arrayB[j]);
         }

         System.out.println(sb.toString());
         try {
             ScriptEngineManager sem = new ScriptEngineManager();
             ScriptEngine se = sem.getEngineByName("JavaScript");
             String myExpression = sb.toString();
             System.out.println("Result: "+se.eval(myExpression));

         } catch (ScriptException e) {
             System.out.println("Invalid Expression");
             e.printStackTrace();

         }
      }  
    }

    // Method to find total number of permutations
    static int calculateTotal(char[] temp, int n) {
      int f = factorial(n);

      // Building HashMap to store frequencies of 
      // all characters.
      HashMap<Character, Integer> hm = 
                       new HashMap<Character, Integer>();
      for (int i = 0; i < temp.length; i++) {
        if (hm.containsKey(temp[i]))
          hm.put(temp[i], hm.get(temp[i]) + 1);
        else
          hm.put(temp[i], 1);
      }

      // Traversing hashmap and finding duplicate elements.
      for (Map.Entry e : hm.entrySet()) {
        Integer x = (Integer)e.getValue();
        if (x > 1) {
          int temp5 = factorial(x);
          f = f / temp5;
        }
      }
      return f;
    }

    static void nextPermutation(char[] temp) {

      // Start traversing from the end and
      // find position 'i-1' of the first character 
      // which is greater than its  successor. 
      int i;
      for (i = temp.length - 1; i > 0; i--)
        if (temp[i] > temp[i - 1])
          break;

      // Finding smallest character after 'i-1' and
      // greater than temp[i-1]
      int min = i;
      int j, x = temp[i - 1];
      for (j = i + 1; j < temp.length; j++)
        if ((temp[j] < temp[min]) && (temp[j] > x))
          min = j;

      // Swapping the above found characters.
      char temp_to_swap;
      temp_to_swap = temp[i - 1];
      temp[i - 1] = temp[min];
      temp[min] = temp_to_swap;

      // Sort all digits from position next to 'i-1'
      // to end of the string.
      Arrays.sort(temp, i, temp.length);

      // Print the String
      print(temp);
    }

    static void printAllPermutations(String s) {

      // Sorting String
      char temp[] = s.toCharArray();
      Arrays.sort(temp);

      // Print first permutation
      print(temp);

      // Finding the total permutations
      int total = calculateTotal(temp, temp.length);
      for (int i = 1; i < total; i++)
        nextPermutation(temp);
    }

    static String zero(int L) {                                      
          return (L <= 0 ? "" : String.format("%0" + L + "d", 0));      
       }                                                               
       static String zeroPad(String s, int L) {                        
          return zero(L - s.length()) + s;                              
       } 

}

给我输出像

||operators:  2
0|0|0= 0
0|0|1= 1
0|1|0= 1
0|1|1= 1
1|0|0= 1
1|0|1= 1
1|1|0= 1
1|1|1= 1
&|operators:  2
0&0|0= 0
0&0|1= 1
0&1|0= 0...........

我需要输出显示为

||operators:  2       &|operators:  2

0|0|0= 0           0&0|0= 0
0|0|1= 1           0&0|1= 1
0|1|0= 1           0&1|0= 0........
0|1|1= 1
1|0|0= 1
1|0|1= 1
1|1|0= 1
1|1|1= 1

如何进行更改,以便输出显示在多列而不是单列中。或者我必须制作一个数组并显示它?

0 个答案:

没有答案
相关问题