java执行字符串中的运算公式

public class runString {  
  
    static ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");  
  
    public static void main(String[] args) {  
        String strs = "1+1*2+(10-(2*(5-3)*(2-1))-4)+10/(5-0)";  
        try {  
            System.out.println(jse.eval(strs));  
        } catch (Exception t) {  
        }  
    }  
}  

 

个人修改

 

public StringfinalResult (String original) {
	try {  
		Object temp = jse.eval(original);
                String result =temp.toString();
                return result;
	} catch (Exception t) {  
    	        return null;
	}
}

 

 

 

你可能感兴趣的:(java执行字符串中的运算公式)