Convert String to operator(+*/-) in java

前端 未结 5 1291
时光说笑
时光说笑 2021-01-06 05:57

I am using Stack class to calculate simple arithmetic expressions involving integers, such as 1+2*3.your program would execute operations in the order given,without regardin

5条回答
  •  盖世英雄少女心
    2021-01-06 06:51

    probably the best way to do it will be equals, but it's best to ignore whitespaces:

    i'm not quite sure how you split your string, but for example, if you have a char op and two integer a and b:

    String str = op.replace(" ", "");
    
    if(str.equals("*")){
       retVal = a*b;
    } else if(str.equals("+")){
       retVal = a+b;
    }//etc
    

提交回复
热议问题