Java根据数学公式计算出结果

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Test {
    public static void main(String[] args) {

        //年均充放电量=(E2*H4*G4*0.95*0.86)*0.871/10000
        String expression = "(200*0.9*0.8+100*2/0.9)*0.871/100";
        try {
            System.out.println(evaluateExpression(expression));
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }

    public static double evaluateExpression(String expression)throws ScriptException {
        ScriptEngineManager manager= new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        return(double)engine.eval(expression);
    }

}

 

posted @ 2024-05-09 17:17  江南大才子  阅读(135)  评论(0编辑  收藏  举报