Boolean.parseboolean 无法评估计算的布尔表达式
Boolean.parseboolean not working to evaluate a computed boolean expression
我正在尝试解决一个 java 程序,我们必须计算一个字符串表达式和 return 真或假。表达式可能像这样 s=(F&F|V|!V&!F&!(F|F&V)) 其中 V 代表真,F 代表假。我认为将 V 替换为 true 并将 F 替换为 false 会起作用。但是输出结果不尽如人意,如果我错了请告诉我。
我的代码是这样的
String temp="";
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch=='V')
{
temp+="true";
continue;
}
else if(ch=='F')
{
temp+="false";
continue;
}
else
{
temp+=ch;
continue;
}
}
boolean t=Boolean.parseBoolean(temp);
out.println(temp);
out.println(t);
return t;
Boolean.parseBoolean(s)
等同于"true".equalsIgnoreCase(s)
;它不评估逻辑表达式。相反,您可以使用 JavaScript 引擎。
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
//...
try {
String s = "(F&F|V|!V&!F&!(F|F&V))";
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
boolean b = Boolean.parseBoolean(engine.eval(s.replace("F", "false")
.replace("V", "true").replace("&", "&&").replace("|", "||")).toString());
System.out.println(b);
} catch(ScriptException e){
// Handle
e.printStackTrace();
}
试试这个。
static boolean evalBooleanExpression(String s) {
return new Object() {
int index = 0;
int ch = get();
int get() { return ch = index < s.length() ? s.charAt(index++) : -1; }
boolean eat(int ex) {
while (Character.isWhitespace(ch))
get();
if (ch != ex) return false;
get();
return true;
}
boolean paren() {
boolean r = expression();
if (!eat(')')) throw new RuntimeException("')' expected");
return r;
}
boolean factor() {
boolean not = false;
if (eat('!')) not = true;
if (eat('V')) return !not;
else if (eat('F')) return not;
else if (eat('(')) return not ^ paren();
else throw new RuntimeException("unknown char '" + ((char)ch) + "'");
}
boolean term() {
boolean r = factor();
while (eat('&'))
r &= factor();
return r;
}
boolean expression() {
boolean r = term();
while (eat('|'))
r |= term();
return r;
}
boolean parse() {
boolean r = expression();
if (ch != -1)
throw new RuntimeException(
"extra string '" + s.substring(index - 1) + "'");
return r;
}
}.parse();
}
和
String s = "(F&F | V |!V&!F&!(F| F&V))";
System.out.println(evalBooleanExpression(s));
输出:
true
我正在尝试解决一个 java 程序,我们必须计算一个字符串表达式和 return 真或假。表达式可能像这样 s=(F&F|V|!V&!F&!(F|F&V)) 其中 V 代表真,F 代表假。我认为将 V 替换为 true 并将 F 替换为 false 会起作用。但是输出结果不尽如人意,如果我错了请告诉我。
我的代码是这样的
String temp="";
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch=='V')
{
temp+="true";
continue;
}
else if(ch=='F')
{
temp+="false";
continue;
}
else
{
temp+=ch;
continue;
}
}
boolean t=Boolean.parseBoolean(temp);
out.println(temp);
out.println(t);
return t;
Boolean.parseBoolean(s)
等同于"true".equalsIgnoreCase(s)
;它不评估逻辑表达式。相反,您可以使用 JavaScript 引擎。
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
//...
try {
String s = "(F&F|V|!V&!F&!(F|F&V))";
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
boolean b = Boolean.parseBoolean(engine.eval(s.replace("F", "false")
.replace("V", "true").replace("&", "&&").replace("|", "||")).toString());
System.out.println(b);
} catch(ScriptException e){
// Handle
e.printStackTrace();
}
试试这个。
static boolean evalBooleanExpression(String s) {
return new Object() {
int index = 0;
int ch = get();
int get() { return ch = index < s.length() ? s.charAt(index++) : -1; }
boolean eat(int ex) {
while (Character.isWhitespace(ch))
get();
if (ch != ex) return false;
get();
return true;
}
boolean paren() {
boolean r = expression();
if (!eat(')')) throw new RuntimeException("')' expected");
return r;
}
boolean factor() {
boolean not = false;
if (eat('!')) not = true;
if (eat('V')) return !not;
else if (eat('F')) return not;
else if (eat('(')) return not ^ paren();
else throw new RuntimeException("unknown char '" + ((char)ch) + "'");
}
boolean term() {
boolean r = factor();
while (eat('&'))
r &= factor();
return r;
}
boolean expression() {
boolean r = term();
while (eat('|'))
r |= term();
return r;
}
boolean parse() {
boolean r = expression();
if (ch != -1)
throw new RuntimeException(
"extra string '" + s.substring(index - 1) + "'");
return r;
}
}.parse();
}
和
String s = "(F&F | V |!V&!F&!(F| F&V))";
System.out.println(evalBooleanExpression(s));
输出:
true