如何计算出字符串 像"3*4+5"的值啊

解决方案 »

  1.   

    jdk1.6可以这样做:
    import javax.script.*;
    public class TestScript {
    public static void main(String[] args) throws Exception {
    String exp = "3*4+5";
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("jexl");
    Object result = engine.eval(exp);

    System.out.println("结果类型:" + result.getClass().getName() + ",计算结果:" + result); }
    }
      

  2.   

    上面是jdk1.5的做法需要下载commons-jexl-2.0.1.jar
    commons-logging-1.1.1.jar
    bsf-api-3.1.jar加到classpath中就可以运行了
      

  3.   

    如果是JDK6以下的版本,解决的办法也有很多。可以对字符串进行解析。
      

  4.   

    jdk1.6可以这样做:
    import javax.script.*;
    public class TestScript {
    public static void main(String[] args) throws Exception {
    String exp = "3*4+5";
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    Object result = engine.eval(exp);

    System.out.println("结果类型:" + result.getClass().getName() + ",计算结果:" + result); }
    }
      

  5.   

    可以动态编译import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;import javax.tools.JavaCompiler;
    import javax.tools.ToolProvider;
    public class DyComp {
    String expr = null; public static double execute(String expr) {
    DyComp dc = new DyComp(expr);
    double result = -99999;
    try {
    result = dc.result();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return result;
    }

    //私有构造
    private DyComp(String expr) {
    this.expr = expr;
    }

    public double result() throws IOException {
    String javafileContent = "import java.io.*;\r\n" +
    "public class ExpreDyComp {" + "\r\n" +
    "public static double cal() {\r\n" +
    "\tdouble var = " + this.expr + ";\r\n" +
    "return var;\r\n" +
    "}\r\n}";
    System.out.println("待生成的文件内容" + "\n" + javafileContent);
    File tmpFile = new File("ExpreDyComp.java");
    tmpFile.createNewFile();
    PrintWriter printWriter = new PrintWriter(new FileWriter(tmpFile));
    printWriter.write(javafileContent);
    printWriter.flush();
    printWriter.close();
    //对文件进行编译
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    int compRes = compiler.run(null, null, null,"-d","bin","ExpreDyComp.java");
    if(compRes != 0) {
    System.out.println("编译出错");
    }

    Class exprClass = null;
    try {
    exprClass = Class.forName("ExpreDyComp");
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    System.err.println("找不到相应类");
    }
    Method m = null;
    try {
    if(exprClass != null) {
    m = exprClass.getMethod("cal");
    }
    } catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (NoSuchMethodException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    if(m != null) {
    try {
    Double d = (Double)m.invoke(null);
    //System.out.println("值为" + d.doubleValue());
    return d.doubleValue();
    } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    return -9999;
    }

    private double readResultToFile(File inFile) throws IOException {
    BufferedReader bf = new BufferedReader(new FileReader(inFile));
    String content = bf.readLine();
    double res = 0;
    if(content != null) {
    res = Double.parseDouble(content);
    } else {
    System.out.println("文件" + inFile.getAbsolutePath() + "无数据");
    }
    bf.close();
    return res;
    }

    public static void main(String args[]) {
    System.out.println("动态编译");
    System.out.println("输入你要计算的表达式");
    InputStreamReader inputReader = new InputStreamReader(System.in);
    BufferedReader bfReader = new BufferedReader(inputReader);
    String expre = null;
    try {
    expre = bfReader.readLine();
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    System.out.println("从键盘输入出现问题");
    e1.printStackTrace();
    }
    //定义结果数据
    double result = -99999;
    result = execute(expre);
    //打印到console
    System.out.println(expre + "计算结果如下" + "\n" + result);
    }
    }