日期:2014-05-20 浏览次数:20744 次
Set keys = (Set) map.keySet();
Iterator iterator = (Iterator) keys.iterator();
String[] item1 = tcaas12.getItem1().split(",");//获得String数组
for (int j = 0; j < item1.length; j++) {
//解析 如果数组item1中得某个元素和给我得Map得Key相等 就把那个元素换成 Map得Value
if(item1[i].trim().equals(iterator.next().toString().trim())){
value = (Double) map.get(item1[i]);//得到对应得值
ecc.setImpNum(value);
item1[i] =String.valueOf(value);
}
}
StringBuilder builder = new StringBuilder();
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 2);
map.put("b", 3);
String[] items = new String[]{"a", "-", "b"};
for (String item : items) {
item = item.trim();
if (item.length() == 1 && "+-*/()".indexOf(item) != -1) {
builder.append(item);
} else if (map.containsKey(item)) {
builder.append(map.get(item));
} else {
builder = new StringBuilder();
System.out.println(item + "没有对应的值");
break;
}
}
String exp = builder.toString();
if (!exp.isEmpty()) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
try {
System.out.println((int) Float.parseFloat(engine.eval("eval ('" + exp + "')").toString()));
} catch (ScriptException ex) {
System.err.println("错误的表达式:" + exp);
}
}