用String類型做判斷條件 我現在有個String a = "row == 1 || row == 2"字符串的長度是不確定的 但是可以組成這樣一個類似的String字符串,我想用這個當作if的判斷條件 該怎么作? 就是要形成這樣的型式if(row == 1 || row == 2){……}先謝謝了哈 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String的可以转成boolean吗?我也学习下。 String a = Boolean.toString(row == 1 || row == 2); 用MVEL包.这个相当于javascript中的EVAL 嗯 Razer~~ He wants to transfer String to boolean... 那 if(a){} ?不还是一个STRING吗。如果if(Boolean.toString(row == 1 || row == 2)){}那不是多此一举?意义何在? Right, crazyI've no idea 了~ 这个目前的 JDK 是没有这个能力的!可以试着用用 Java 编写的动态语言下载 BeanShell 动态语言 http://www.beanshell.org/import bsh.EvalError;import bsh.Interpreter;public class Test1 { public static void main(String[] args) throws EvalError { int row = 2; String a = "row == 1 || row == 2"; boolean result = false; Interpreter bsh = new Interpreter(); bsh.set("row", row); bsh.eval("result = " + a); result = ((Boolean)bsh.get("result")).booleanValue(); if(result) { System.out.println("true"); } else { System.out.println("false"); } }}row 的值是事先知道的,使用 Interpreter#set 方法设置一下就行了。 if(Boolean.parseBoolean(String s) ) 4 楼说的 MVEL 也是类似功能的http://mvel.codehaus.org/ 改成这样更好一些:import bsh.EvalError;import bsh.Interpreter;public class Test1 { public static void main(String[] args) throws EvalError { int row = 2; String a = "row == 1 || row == 2"; Interpreter bsh = new Interpreter(); bsh.set("row", row); boolean result = ((Boolean)bsh.eval(a)).booleanValue(); if(result) { System.out.println("true"); } else { System.out.println("false"); } }} 为什么"重写的方法不能比被重写的方法具有更严格的访问权限" 数组下标溢出的问题。 BMP图片的处理想请教各位高人! List.removeall的效率问题? 怎么判断一个字符是否为空字符? 在哪里下载jspSmartUpload? 简单问题:怎么让我的定时任务连续执行?过来看看,很有意思。 能具体讲一下吗?(我是新手) 关于applet中用jdbc的问题! 如何将泛型当作参数传递? swing中如何从一个JFrame跳到另一个JFrame?急! PrintStream的弊端!
嗯 Razer~~
if(a){
} ?
不还是一个STRING吗。
如果
if(Boolean.toString(row == 1 || row == 2)){
}
那不是多此一举?
意义何在?
Right,
crazy
I've no idea 了~
import bsh.Interpreter;public class Test1 {
public static void main(String[] args) throws EvalError {
int row = 2;
String a = "row == 1 || row == 2"; boolean result = false;
Interpreter bsh = new Interpreter();
bsh.set("row", row);
bsh.eval("result = " + a);
result = ((Boolean)bsh.get("result")).booleanValue(); if(result) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}row 的值是事先知道的,使用 Interpreter#set 方法设置一下就行了。
import bsh.Interpreter;public class Test1 {
public static void main(String[] args) throws EvalError {
int row = 2;
String a = "row == 1 || row == 2";
Interpreter bsh = new Interpreter();
bsh.set("row", row);
boolean result = ((Boolean)bsh.eval(a)).booleanValue();
if(result) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}