JUEL mathematics



我在java上编程服务器,需要在JUEL的帮助下使用数学。

例如2*2+2,它将打印出结果6。问题是,它不起作用。

这里有一种数学方法:

public static void Math(String operation){
ValueExpression expr = f.createValueExpression(cont, "${" + operation + "}", Object.class);
out.print(expr);
}

所以,我将一个操作作为String"8+5+4",并在conlsole上打印出结果。

我可能用错JUEL了吗?

try{
while ((newLine = in.readLine()) != null) {
try {
Pattern patternMath = Pattern.compile("<MATH>(\s*?)(\d+)(\s*?)([-+/\*])(\s*?)(\d+)");
Matcher matcher = patternMath.matcher(newLine);
Pattern pattCSEN = Pattern.compile("<RU-EN>(.*)");
Matcher matCSEN = pattCSEN.matcher(newLine);
Pattern pattENCS = Pattern.compile("<EN-RU>(.*)");
Matcher matENCS = pattENCS.matcher(newLine);


if (matENCS.find()) {out.println("<TRANSLATION> " + ENGtoCZ(matENCS.group(1)));}
else if (matCSEN.find()) {out.println("<TRANSLATION> " + CZtoENG(matCSEN.group(1)));}
else if (newLine.equals("<BYE>")) {
clientSocket.close();
}
else  if (matcher.find()) {
Math(newLine);
} else {
out.println("<FAIL>");
clientSocket.close();
}
}catch(Exception ex){
clientSocket.close();;
}}
} catch (Exception e) {
out.println("<FAIL>");
clientSocket.close();
}
}

以下几行始终确保代码中包含。确保你已经设置了如下的上下文:

SimpleContext context = new SimpleContext();

表达式对象应该是Implemented类,类似于以下内容:

ExpressionFactory factory = new ExpressionFactoryImpl();

JUEL Api

其次,将ValueExpression放入try块中,以防出现任何问题,您将从捕获的异常中了解问题。

检查如何使用System.out输入表达式,如中所示

Object display = expr.getValue(context);
System.out.println("> " + display);

这里是

最新更新