我正在尝试用Java制作计算器GUI,但是我不知道如何将“ 7/7”转换为双精度。
我尝试将其直接转换为double,但是无法正常工作。
private class handler implements ActionListener{
String answerString = "";
int answerDouble;
public void actionPerformed(ActionEvent e) {
if(e.getSource() == clear) {
answerString = "";
result.setText(answerString);
}
if(e.getSource() == openBracket) {
answerString = answerString + "(";
result.setText(answerString);
}
if(e.getSource() == closeBracket) {
answerString = answerString + ")";
result.setText(answerString);
}
if(e.getSource() == divide) {
answerString = answerString + "/";
result.setText(answerString);
}
if(e.getSource() == seven) {
answerString = answerString + "7";
result.setText(answerString);
}
if(e.getSource() == equal) {
answerDouble = Integer.parseInt(answerString);
answerString = Double.toString(answerDouble);
result.setText(answerString);
}
}
}
对于7/7,它应该显示1,但在命令行上输出错误。
答案 0 :(得分:0)
您可以使用 JavaScript ┌──────────┬────────────┐
│ (index) │ Values │
├──────────┼────────────┤
│ 100194 │ 3 │
│ 100249 │ 400 │
│ 100268 │ 3 │
│ 100302 │ 95 │
│ 100328 │ 499 │
│ feasible │ true │
│ result │ 1744830.33 │
│ bounded │ true │
│ D100268 │ 1 │
└──────────┴────────────┘
来评估此类表达式:
ScriptEngine
输出:
String expression = "7/7";
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
System.out.println(scriptEngine.eval(expression)); // you will have to handle ScriptException