2014-03-26 35 views
0

确定,所以可变current是操作员做算术(+ - * /%)爪哇使用可变运营

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

我的数需要执行数学运算t2 current t1(t2“operator”t1,基本上)

你会如何去做这件事。提前致谢!

+0

http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form –

+3

你尝试过什么?将字符串转换为“int”或“float”或其他类型,确定哪个运算符使用String.equals并执行操作。 – clcto

+0

“+”是运算符,“+”是字符串。当你想要连接字符串't2'和't1'时't2 current t1'错误,'t2 + t1'正确。为了在字符串中添加数字,您必须首先将字符串转换为数字并添加。 –

回答

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.... 

}