首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将string If语句转换为Java IF语句

将string If语句转换为Java IF语句
EN

Stack Overflow用户
提问于 2012-08-17 17:08:07
回答 3查看 258关注 0票数 0

我需要将字符串转换为

代码语言:javascript
复制
class freud IF : 0.010<=cst0.1<=0.02 ^ 0.012<=cst0.2<=0.014 ^ 0.01<=cst0.3<=0.011 ^ 0.009<=cst0.4<=0.01 ^ cst0.5=0.009 ^ 0.008<=cst0.6<=0.009 ^ 0.008<=cst0.7<=0.009 ^ 0.007<=cst0.8<=0.009 ^ 0.007<=cst0.9<=0.009 ^ 0.008<=cst1.0<=0.012

转换为实际的Java if语句:即

代码语言:javascript
复制
if(0.010 <= cst0.1 && cst0.1 <= 0.02 .....)

其中cst0.1成为一个浮点数的名称,等等。

有什么想法吗?我已经尝试将字符串拆分成它的组成部分,但我需要的所有字符串都不同!

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-17 17:59:53

您可以尝试创建evaluate method boolean evaluate(String stringToEvaluate),它会将字符串拆分成段并计算每一位

代码语言:javascript
复制
public class MyClass {
    public boolean evaluate(String stringToEvaluate)
    {
    boolean exprvalue = true;
    for (String string : stringToEvaluate.split("^"))
            {
            exprvalue = exprvalue && evaluateLE(string);
            }
    return exprvalue;
    }

    private boolean evaluateLE(String string) {
        String[] values = string.split("<=");
        if (values.length==2)
        {
            return evaluateVal(values[0]) <= evaluateVal(values[1]); 
        }

        return evaluateOtherLogicalExpr(string);
    }

    private float evaluateVal(String string) {
        if (isExpresion(string)) return evaluateArthmeticalExpr(string);
        if (isVariable(string)) return evaluateVariable(string);

        return Float.parseFloat(string);
    }
}
票数 0
EN

Stack Overflow用户

发布于 2012-08-17 17:14:32

从您所展示的内容可以看出,在cstX.Y形式的字符串中,用一个有效的标识符字符替换点号就足够了,例如_,其中X和Y是数字,并将^替换为&&

要使生成的if语句有效,还应该声明引入的所有cstX_Y变量。

票数 0
EN

Stack Overflow用户

发布于 2012-08-17 18:08:51

我想我有答案了。下面是我的步骤和代码:

  1. 确定行中是否有if,如果有,则使用split(“^ ")将行拆分成相等组件-这将随结构的不同而不同。
  2. 现在签入每个组件,检查是否存在相等"<=",”=“
  3. 根据structure.
  4. convert单独处理每个"0.025”(例如) to一个浮动代码<

>H111>基于3,5的create JAVA if语句,以及如果所有等式在整个行中都成立,则将其与要比较的数据进行比较的数据。

  1. 现在检查每个组件,检查是否存在相等“<=”,“=”
  2. 分别处理每个“0.025”(例如) to a float

<>H111>如果一个失败了,就离开,换一条新的线。If all holds函数返回初始值,即Artist string。

不管怎样,下面是代码:

代码语言:javascript
复制
while(line != null ){
            boolean equality = false;

            String[] split_line;
            String[] first_part;
            String[] statements;
            String[] components;

            if(line.contains("IF")){
                split_line = line.split(":");
                statements = split_line[1].split(" ^ ");
                int attrib_pos = 0;

                for(int pos = 0; pos < statements.length; pos++){
                    if(statements[pos].contains("<=")){
                        components = statements[pos].split("<=");
                        float component_1 = new Float(components[0]);
                        // middle component is attribute
                        float component_2 = new Float(components[2]);

                        if(!(( component_1 <= data[attrib_pos]) && (data[attrib_pos] <= component_1))){
                            break;
                        }
                    }
                    else if(statements[pos].contains("=") ){
                        components = statements[pos].split("=");
                        float component_1 = new Float(components[1]);
                        if(!(component_1 == data[attrib_pos])){
                            break;
                    }
                        // if we have not found a false statement, increase no of attributes which hold
                        attrib_pos++;
                }
                }
                // if we have run through all attributes as holding then return, as we have found a true rule
                if(attrib_pos-1 == statements.length){
                    _return = (split_line[0].split("\\s+"))[2];
                    return _return;
                }
            }
            // once we have read the line, read another 
        line = read.readLine();

        }               
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12002906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档