Java String to complete mathematical sum

后端 未结 3 682
礼貌的吻别
礼貌的吻别 2021-01-28 07:24

In java, I am currently in the process of trying to find a way to do mathematical sums by typing in text to a JTextField, putting it into a String, and converting it to an int a

3条回答
  •  面向向阳花
    2021-01-28 08:02

    This will do it for you.

      import javax.script.ScriptEngineManager;
        import javax.script.ScriptEngine;
    
        public class Test {
          public static void main(String[] args) throws Exception{
            ScriptEngineManager mgr = new ScriptEngineManager();
            ScriptEngine engine = mgr.getEngineByName("JavaScript");
            String foo = "40+2";//any mathematical operations +,-,*,/ 
            System.out.println(engine.eval(foo));
            } 
        }
    

    Here you can put the methods describing the log/cos etc functions

    String st = "10+3";
    int result;
    for(int i=0;i

提交回复
热议问题