使用 Java 隔开控制台文本,不带制表符



嘿,我目前正在用Java编写一个表达式计算器。我现在已经让它工作了,但无法弄清楚如何在终端中间隔写我想要的是两个相邻的列表。我最初会使用选项卡,但由于数组可以增长,它会移动另一个选项卡。这是我现在得到的:

OUTPUT BUFFER               STACK
[]                          []
[(]                         []
[(]                         [4]
[(, +]                      [4]
[(, +, (]                   [4]
[(, +, (]                   [4, 2]
[(, +, (, *]                    [4, 2]
[(, +, (, *, (]                 [4, 2]
[(, +, (, *, (]                 [4, 2, -2]
[(, +, (, *, (, -]                  [4, 2, -2]
[(, +, (, *, (, -]                  [4, 2, -2, 1]
[(, +, (, *]                    [4, 2, -2, 1, -]
[(, +]                     [4, 2, -2, 1, -, *]
[]                         [4, 2, -2, 1, -, *, +]
[*]                        [4, 2, -2, 1, -, *, +]
[*]                        [4, 2, -2, 1, -, *, +, 1.5]
[%]                        [4, 2, -2, 1, -, *, +, 1.5, *]
FINAL OUTPUT: [4, 2, -2, 1, -, *, +, 1.5, *, 2, %]
INFIX: ( 4 + ( 2 * ( -2 - 1 ) ) ) * 1.5 % 2 
POSTFIX: 4 2 -2 1 - * + 1.5 * 2 % 
TOKEN   ACTION      STACK
4      Add             [4.0]
2      Add             [4.0, 2.0]
-2     Add             [4.0, 2.0, -2.0]
1      Add             [4.0, 2.0, -2.0, 1.0]
-      Operate         [4.0, 2.0, -3.0]
*      Operate         [4.0, -6.0]
+      Operate         [-2.0]
1.5    Add             [-2.0, 1.5]
*      Operate         [-3.0]
2      Add             [-3.0, 2.0]
%      Operate         [-1.0]
ANSWER: -1.0

以下是我想要实现的目标:

OUTPUT BUFFER               STACK
[]                          []
[(]                         []
[(]                         [4]
[(, +]                      [4]
[(, +, (]                   [4]
[(, +, (]                   [4, 2]
[(, +, (, *]                [4, 2]
[(, +, (, *, (]             [4, 2]
[(, +, (, *, (]             [4, 2, -2]
[(, +, (, *, (, -]          [4, 2, -2]
[(, +, (, *, (, -]          [4, 2, -2, 1]
[(, +, (, *]                [4, 2, -2, 1, -]
[(, +]                      [4, 2, -2, 1, -, *]
[]                          [4, 2, -2, 1, -, *, +]
[*]                         [4, 2, -2, 1, -, *, +]
[*]                         [4, 2, -2, 1, -, *, +, 1.5]
[%]                         [4, 2, -2, 1, -, *, +, 1.5, *]
FINAL OUTPUT: [4, 2, -2, 1, -, *, +, 1.5, *, 2, %]
INFIX: ( 4 + ( 2 * ( -2 - 1 ) ) ) * 1.5 % 2 
POSTFIX: 4 2 -2 1 - * + 1.5 * 2 % 
TOKEN   ACTION      STACK
4      Add           [4.0]
2      Add           [4.0, 2.0]
-2     Add           [4.0, 2.0, -2.0]
1      Add           [4.0, 2.0, -2.0, 1.0]
-      Operate       [4.0, 2.0, -3.0]
*      Operate       [4.0, -6.0]
+      Operate       [-2.0]
1.5    Add           [-2.0, 1.5]
*      Operate       [-3.0]
2      Add           [-3.0, 2.0]
%      Operate       [-1.0]
ANSWER: -1.0

这是我当前用于打印堆栈和输出缓冲区的代码:

System.out.println("OUTPUT BUFFERttttSTACK");
ArrayList<String> out = new ArrayList<String>();
Stack<String> stack = new Stack<String>();
for(String token : inputTokens){
    System.out.print(stack+ "ttttt");
    System.out.println(out);
    if(isOperator(token)){
        while(!stack.empty() && isOperator(stack.peek())){
            if (checkPri(token, stack.peek())<=0 || checkPri(token, stack.peek()) < 0 ){
                out.add(stack.pop());
                //System.out.println("OUTPUT: " + out);
                continue;
            }
            break;
        }
        stack.push(token);
    }else if(token.equals("(")){
        stack.push(token);
    }else if(token.equals(")")){
        while(!stack.empty() && !stack.peek().equals("(")){
            out.add(stack.pop());
        }
        stack.pop();
    } else {
        out.add(token);
        //System.out.println("OUTPUT BUFFER: " + out);
    }
}
您可以使用String.format(String,args...

)或System.out.printf(String,args...);

由于我的英语不好,我不知道如何解释,但这里有一个例子:

int i=10000,j=200,k=5000000;
System.out.printf("%10d%10d%10d",i,j,k);

将获得以下输出:

10000     200       5000000
^         ^         ^each has ten spaces between them because of %10d

相关内容

  • 没有找到相关文章

最新更新