将某一表达式放入一个String中,其中会带有"&&"和"||"的判断,然后判断这个表达式是否成立
如下:
String a = "(50-20>10)&&(2+3>0)||(10-1<10)"if(a){
   //正确......
}else{
   //错误......
}请教各位高手谁有思路实现这种程序,谢谢了

解决方案 »

  1.   

    把a当成 X&&Y||Z这种表达式,就是先把表达式看作是逻辑表达式。然后解析。
    然后再解析X,Y,Z...算术表达式。
    表达式的解析在任何一本数据结构的书上都能找到关键词:“栈”
      

  2.   

    使用beanshell 工具,动态执行字符串中的语句
      

  3.   

    那么直接用js eval()就可以,Java是可以执行JS代码的,JS中也可以有java代码
      

  4.   

    eval()有相关的例子可以参考么,如何使用的
      

  5.   

    Boolean a = (50-20>10)&&(2+3>0)||(10-1<10);if(a){
      //正确......
    }else{
      //错误......
    }这样修改之后就可以实现相同的功能!如果纯粹是为了学习,可以试着做一下!beanutils
      

  6.   

    JEXL就是干这个事情的,可以直接计算你string中的表达式
      

  7.   

        public static void main(String... args) throws ScriptException {
            // create a script engine manager
            ScriptEngineManager factory = new ScriptEngineManager();        // create a JavaScript engine
            ScriptEngine engine = factory.getEngineByName("JavaScript");        // evaluate JavaScript code from String
            final boolean b = (Boolean)engine.eval("(50-20>10)&&(2+3>0)||(10-1<10))");
            
            System.out.println(b);
        }
      

  8.   

    给段之前用C#写过的这种算法,不但包括&&,||,还有(),楼主可以参考改成java。         private static string trueCase = "TRUE";
            private static string falseCase = "FALSE";
            private static string andCase = "&&";
            private static string orCase = "||";
            private static char spaceCase = ' ';
            private static char leftParenthesesCase = '(';
            private static char rightParenthesesCase = ')';        static void Main()
            {
                string expression = "A &&((B || C) && D OR E) && F && (G || H)";
             
                Dictionary<string, bool> dict = new Dictionary<string, bool>();
                dict.Add(trueCase, true);
                dict.Add(falseCase, false);
                dict.Add("A", true);
                dict.Add("B", false);
                dict.Add("C", false);
                dict.Add("D", true);
                dict.Add("E", true);
                dict.Add("F", true);
                dict.Add("G", false);
                dict.Add("H", false);            string result = ProcessLogicExpression(expression,dict);
                Console.WriteLine("Result is : " + result);
                Console.Read();
            }        static string ProcessLogicExpression(string expression, Dictionary<string, bool> dict)
            {
                while (expression.Contains(leftParenthesesCase))
                {
                    int left = 0, right = 0;
                    bool matchRightParentheses = false;
                    for (int i = 0; i < expression.Length; i++)
                    {
                        if (expression[i].Equals(leftParenthesesCase))
                        {
                            left = i;
                            for (int j = i + 1; j < expression.Length; j++)
                            {
                                if (expression[j].Equals(leftParenthesesCase))
                                {
                                    left = j;
                                    i = j;
                                }                            if (expression[j].Equals(rightParenthesesCase))
                                {
                                    right = j;
                                    matchRightParentheses = true;
                                    break;
                                }
                            }                        if (matchRightParentheses) break;
                        }
                    }                int beginIndex = left + 1, length = right - left - 1;
                    string subExpression = expression.Substring(beginIndex, length);
                    string oriStr = leftParenthesesCase + subExpression + rightParenthesesCase;
                    string curStr = ProcessLogicExpression(subExpression, dict);
                    expression = expression.Replace(oriStr, curStr);
                }            expression = ProcessByLogicCase(expression, dict, andCase);
                expression = ProcessByLogicCase(expression, dict, orCase);            return expression;
            }        static string ProcessByLogicCase(string expression, Dictionary<string, bool> dict, string logicCase)
            {
                while (expression.Contains(logicCase))
                {
                    string[] strArray = expression.Split(spaceCase);
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (strArray[i].Equals(logicCase))
                        {
                            bool bResult;
                            if (logicCase.Equals(andCase))
                            {
                                bResult = dict[strArray[i - 1]] && dict[strArray[i + 1]];
                            }
                            else
                            {
                                bResult = dict[strArray[i - 1]] || dict[strArray[i + 1]];
                            }                        string oriStr = strArray[i - 1] + spaceCase + logicCase + spaceCase + strArray[i + 1];
                            string curStr = bResult.ToString().ToUpper();
                            expression = expression.Replace(oriStr, curStr);
                            
                            break;
                        }
                    }
                }            return expression;
            }
      

  9.   


    +1
    jdk1.6以上,可以使用这个
    或者可以自己写个语法分析器并执行结果
      

  10.   

    我的是1.4的JDK,看来用不了ScriptEngine 这种方式了