Java使用变量运算符进行算术运算

时间:2014-03-26 16:09:37

标签: java

好,所以变量current是一个运算符(+ - * /%)

t1 = stack.pop()是字符串形成中的数字 t2 = stack.pop()也是字符串形成中的数字

我需要进行数学运算t2 current t1(t2"运算符" t1,基本上)

你会怎么做呢?提前谢谢!

1 个答案:

答案 0 :(得分:3)

简单的事情:

enum operation {
    SUM,
    SUB,
    DIV,
    MUL,
}

static String performOperation(operation op, String t1, String t2) {
   // parse t1 & t2 into integers or whatever you use
   switch(op) {
   case SUM:
       //do sum
       break;
   case SUB:
       //do substraction
       break;

   // handle all of them....

}