【问题标题】:Java - How to pass a paramter to an if statement "if "F(x)" found, pass "x" to the F functionJava - 如何将参数传递给 if 语句“如果找到“F(x)”,则将“x”传递给 F 函数
【发布时间】:2012-05-09 16:30:08
【问题描述】:

我正在做的是以有限字母字符串的形式制定生产规则,将其复制到 char 数组中,然后通过 if 语句运行它,该语句根据字符调用函数。

如“lff[f]”调用functionL、functionF、functionF、functionOpenB、functionF、functionCloseB。

所以目前是:

workRuleArr= stringProdrule.toCharArray();
for  (char c=0; c < workRuleArr.length; c++){
    if (workRuleArr[c] == 'f')
     {
          functionF();

      }
      if (workRuleArr[c] == 'l')
     {
          functionL();

      }

这很好并且可以工作,但是:

我如何将参数从生产规则传递给那些函数,例如“l(100)ff..”,以便它调用 functionL(x)... 其中 x = 100 并将 100 传递给函数? ?

在同一个生产规则字符串中可能有许多不同的 x 值。用户在程序开始时一次性输入规则,因此需要在同一生产规则中处理多个参数

如果问题不清楚,请告诉我任何想法。谢谢

【问题讨论】:

  • 哦,除了我的回答还有一个一般提示:使用 equals(..) 而不是 ==。
  • 不要使用char 作为计数器的类型。使用int

标签: java function if-statement parameter-passing


【解决方案1】:

看起来您需要某种能够为您提供有效令牌的解析器,或者您需要自己动手操作。无论如何,您都需要在阵列中领先一步。 这是另一个实现:

String rule = "FLL(123)FKF";

String pattern = "[a-zA-Z]{1}|[(\\d)]+"; //any single character or set of (numbers)
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(rule);

String command = "", param = "", token;

while(m.find()){
  token = m.group();

  if(token.length() > 1){  //must be parameter
    param = token.substring(1, token.length()-1);
    continue;
  }

  if(command != ""){
    runCommand(command, param);
    param = "";  //clear
  }
  command = token;  //set next
}

if(command != "")    //last trailing run
  runCommand(command, param);  

还需要定义runCommand:

bool runCommand(string command, string param){
  System.out.println("execute function" + command + "(" + param + ")");

  bool success, hasParam = (param != "");
  int p = Integer.parseInt(param);

  switch(command){
    case "F":
      success = (hasParam ? functionF() : functionF(p));
      break;
    case "L":
      success = (hasParam ? functionL() : functionL(p));
      break;
    case "K":
      success = (hasParam ? functionK() : functionK(p));
      break;
  }
  return success;
}

【讨论】:

    【解决方案2】:

    看看Command Pattern。你基本上是在问这个问题的答案

    【讨论】:

      【解决方案3】:

      我不会转换成 char 数组,因为 String 有更多有用的方法,在这种情况下也是如此。

      因此,对于索引为i 的每个字符,您需要测试i+1 处的字符是否为'(',然后使用int k = stringProdrule.indexOf(')', i+2) 获取右括号并将两个括号之间的字符串作为参数传递给函数。然后在k+1 继续。

      【讨论】:

        【解决方案4】:

        在我看来,您拥有的那些“规则功能”都具有相似的目的,因此它们遵循相同的界面, 喜欢

        interface Rule{
            void executeRule();
        }
        

        然后你有不同的规则来实现那个接口,比如

        class RuleF implements Rule{
                void executeRule(){
                    //execute rule F
                }
        }
        
        class RuleL implements Rule{
                void executeRule(){
                    //execute rule L
                }
        }
        

        那么您需要一种简单的方法来将字符与给定规则相关联。 使用 HashMap,例如:

        Map<Character, Rule> ruleMap = new HashMap<Character, Rule>();
        ruleMap.add('f', new RuleF());
        ruleMap.add('l', new RuleL());
        

        这样您就可以删除所有那些“如果”,例如

        workRuleArr= stringProdrule.toCharArray();
        for  (char c=0; c < workRuleArr.length; c++){
            Rule rule = ruleMap.get(workRuleArr[c]);
            rule.executeRule();
        }
        

        现在,如果您需要 Rule 接口接收参数, 您还需要一个类似的 Map 对象来将角色与您需要传递的参数相关联。

        您到底在构建什么?某种状态机?

        干杯,祝你好运! :-)

        【讨论】:

          【解决方案5】:

          尝试一个简单的 if 语句来测试是否有参数:

          workRuleArr= stringProdrule.toCharArray();
          
          for  (char c=0; c < workRuleArr.length; c++){
          
              if (workRuleArr[c] == 'f') {
          
                  if(workRuleArr[c+1].equals("(")) {
          
                      // use the parameter
                      String param = "";
          
                      c++;
          
                      while(!workRuleArr[c+1].equals(")")) {
                          param += workRuleArr[c+1];
                          c++;
                      }
          
                      int yourParameter = Integer.parseInt(param);
          
                      functionF(yourParameter);
                  }
                  else {
          
                      functionF();
                  }
              }
              if (workRuleArr[c] == 'l'){
          
                  functionL();
              }
          }
          

          请注意,我没有测试代码,可能会有一些错误。

          【讨论】:

            猜你喜欢
            • 2019-04-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多