将插入的字符串转换为可编译的表达式 [英] Converting inserted string to a compilable expression

查看:109
本文介绍了将插入的字符串转换为可编译的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,所以我的问题看起来很微不足道,但是我在书中的任何地方找不到答案。

I'm somehow new to Java so my question can seem trivial, however i cannot find an answer to it anywhere in my books.

我想启动一个对话框,在其中他将输入算术表达式(例如(2 * x + y))然后打印出这种表达式的结果(对于给定的x和y值)

I want to initiatie a dialog with a user in which he would enter an arithmetic expression (ex. (2*x+y)) And then print out the result for such expression (for given values of x and y)

    String EXPRESION = null;
    EXPRESION = JOptionPane.showInputDialog("Please enter expresion for dy/dx");
    double x = 1.4;
    double y = 5;
    double output = HERE IS THE PROBLEM
    JOptionPane.showMessageDialog("The value is" +output);

我无法转换用户插入的字符串,因此它将用作x的算术表达式, y,因此定义输出的值

I fail to convert the string inserted by the user so it would serve as an arithmetic expression for x and y and hence define the value for output

TIA
JMT

TIA JMT

推荐答案

自己解析它并不困难,但如果你需要快速和可能准确的结果,你可以使用一个现有的库来解析算术表达式,如 JEP 或者简单解析器的算术表达式

It isn't so hard to parse them yourself, but if you need fast and probably accurate result, you can use one of the existing libraries to parse arithmetic expressions, like JEP or perhaps that Simple Parser for Arithmetic Expressions.

a href =http://www.singsurf.org/djep/jep.html =nofollow noreferrertitle =Jep Console> Jep Console 。
看起来像Jep是payware。它可能不是一个问题,否则我写道,你应该能够找到一些等效的图书馆。

See the Jep Console for a sample. It looks like Jep is payware. It might not be a problem, otherwise as I wrote, you should be able to find a number of equivalent libraries.

这篇关于将插入的字符串转换为可编译的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆