C++里有函数指针,然后用指针数组实现。
不知道JAVA里如何实现。
不知道JAVA里如何实现。
解决方案 »
- 不执行程度看哪里有错
- 请教一个Java调用dll的问题,请各位高手帮忙看看,谢谢
- weblogic下如何配置xerces,急!!!!
- 使用Eclipse的SWT开发WEB应用--SmartSWT RIA平台测试版发布
- 请问各位大侠一个小问题
- 帮我看看二分法查找的问题
- 一个菜鸟问题:如何创建一个固定大小的frame
- JComboBox展开的情况下其它组件无法获得鼠标点击事件,触发的鼠标的事件会被用来处理JComboBox弹出部分的隐藏
- xiaomin0613(小小的疲倦的鱼) 近来,我向你赔罪,20分小小意思,请收下。
- 文件不能删除的原因。急!
- 一个关于file.exists方法的问题
- java.util.ConcurrentModificationException
JAVA里貌似是只能用曲线救国了
interface Operator {
public int compute(int op1, int op2);
}class Add implements Operator {
public int compute(int op1, int op2) {
return op1 + op2;
}
}...Operator[] ops;...for(Operator op : ops) {
...
z = op.compute(x, y);
...
}
2 就事论事,类似你的穷举法import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;public class Test { public static void main(String[] args) throws Exception {
String[] operators = new String[] {
"+", "-", "*", "/"
};
int[] numbers = new int[] {
1, 2, 3, 4
};
for (int i = 0; i < operators.length; i++) {
for (int j = 0; j < operators.length; j++) {
for (int k = 0; k < operators.length; k++) {
String script = String.format("%d %s %d %s %d %s %d",
numbers[0],
operators[i],
numbers[1],
operators[j],
numbers[2],
operators[k],
numbers[3]);
if (eval(script) == 24) {
System.out.println(script);
return;
}
}
}
}
System.err.println("Oooops!");
} static final ScriptEngine engine;
static {
ScriptEngineManager mgr = new ScriptEngineManager();
engine = mgr.getEngineByExtension("js");
} static double eval(String script) throws Exception {
double doubleValue = ((Number) engine.eval(script)).doubleValue();
return doubleValue;
}
}