[英]How To Solve A Basic Math Equation In A String In Java?
我设置了一个基本的计算器,因此当按下等号按钮时,JTextField以字符串形式保存,这就是问题所在,它是字符串形式,所以我不知道如何求解方程式。 我尝试使用JavaScript引擎,但无法弄清楚。 有什么帮助吗?
//actionListener
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(oneButton)){
input1 = 1;
text.setText(text.getText()+input1);}
else if (e.getSource().equals(twoButton)){
input2 = 2;
text.setText(text.getText()+input2);}
else if(e.getSource().equals(threeButton)){
input3 = 3;
text.setText(text.getText()+input3);}
else if(e.getSource().equals(fourButton)){
input4 = 4;
text.setText(text.getText()+input4);}
else if(e.getSource().equals(fiveButton)){
input5 = 5;
text.setText(text.getText()+input5);}
else if(e.getSource().equals(sixButton)){
input6 = 6;
text.setText(text.getText()+input6);}
else if(e.getSource().equals(sevenButton)){
input7 = 7;
text.setText(text.getText()+input7);}
else if(e.getSource().equals(eightButton)){
input8 = 8;
text.setText(text.getText()+input8);}
else if(e.getSource().equals(nineButton)){
input9 = 9;
text.setText(text.getText()+input9);}
else if(e.getSource().equals(zeroButton)){
input0 = 0;
text.setText(text.getText()+input0);}
else if(e.getSource().equals(plusButton)){
text.setText(text.getText()+" + ");}
else if(e.getSource().equals(minusButton)){
text.setText(text.getText()+" - ");}
else if(e.getSource().equals(timesButton)){
text.setText(text.getText()+" * ");}
else if(e.getSource().equals(dividButton)){
text.setText(text.getText()+" / ");}
else if(e.getSource().equals(clrButton)){
text.setText("");}
else if(e.getSource().equals(enterButton)){
eq = text.getText();
//trying the javascript ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
try {
text.setText((String) engine.eval(eq));
} catch (ScriptException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
如果操作顺序很重要,则必须使用堆栈。 如果仅从左到右进行计算,那应该没问题。
就像@Ericson Willians回答的那样,您可以通过以下方式将String转换为int或double:
String str = text.getText();
Integer.parseInt(str);
Double.parseDouble(str);
但是,您可能会在这里遇到麻烦,因为当您按下Enter键时,您将获得放置在文本元素中的整个方程式,也就是如果您按下Enter键并且方程式存储为“ 2 + 3” ,您的text.getText()将为您提供“ 2 + 3”,并且如果没有错误,您将无法使用以下内容。
Integer.parseInt(text.getText()); //NumberFormatException For input string: "2 + 3"
Double.parseDouble(text.getText()); //NumberFormatException for "2 + 3"
在将字符串转换为整数或双精度数之前,您将必须以特定方式分解字符串。 拆分字符串的一种好方法是使用类似以下内容的方法:
String st = text.getText();
//Example: String st = "2 + 3";
//remove white space
st = st.replaceAll("\\s","");
//might have to change to allow for negative numbers
String[] splitStrings = (st.split("((?<=[+-/*])|(?=[+-/*]))"));
//Example: splitStrings is now -> ["2","+","3"]
这使操作之间的字符串破裂。 然后,您可以获取所需的元素并将其解析为整数。 因此,例如:
int result;
int left = Integer.parseInt(splitStrings[0]);
String op = splitStrings[1];
int right = Integer.parseInt(splitStrings[2]);
if(op.equals("+")){
result = left + right;
}
text.setText("" + result);
但是,由于等式可能很大,因此您可能应该创建一个执行类似此操作的循环,并且所有操作都需要使用if语句或switch语句。
您可以编写一个使用antlr解析字符串的计算器: http ://www.antlr.org/
您为什么不只使用Integer和Double类的parse方法?
int integerNumber = Integer.parseInt(text.getText());
double doubleNumber = Double.parseDouble(text.getText());
如您所见,Integer类中的方法parseInt()将字符串转换为Integer,而Double也是如此。 完成此操作后,您就可以使用字符串完美地进行任何数学运算了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.