Java 编写创建随机表达式的递归方法

Java 编写创建随机表达式的递归方法,java,Java,我应该编写一个递归方法,用于在表达式树中生成表达式。该方法应该有一个限制表达式树“高度”的参数。maxHeight给出了最大允许高度,而不是实际高度。常量、变量和二进制运算符的节点已经创建,但我在使该方法工作时遇到了问题。下面是我创建的方法和一个树节点。我还在学习Java,所以请善待我。任何帮助都将不胜感激 static ExpNode randomExpression(int maxHeight) { ExpNode e1 = new BinOpNode('+', new Va

我应该编写一个递归方法,用于在表达式树中生成表达式。该方法应该有一个限制表达式树“高度”的参数。maxHeight给出了最大允许高度,而不是实际高度。常量、变量和二进制运算符的节点已经创建,但我在使该方法工作时遇到了问题。下面是我创建的方法和一个树节点。我还在学习Java,所以请善待我。任何帮助都将不胜感激

static ExpNode randomExpression(int maxHeight) {
        ExpNode e1 = new BinOpNode('+', new VariableNode(), new ConstNode(maxHeight));
        ExpNode e2 = new BinOpNode('*', new ConstNode(maxHeight), new VariableNode());
        ExpNode e3 = new BinOpNode('*', e1, e2);
        ExpNode e4 = new BinOpNode('-', e1, new ConstNode(-3));
        if (maxHeight < 0) {
            return null;
        }
        if (maxHeight == 0) {           
            
            return new BinOpNode('-', e1, e2);
        } 
        if (maxHeight > 0) {            
            maxHeight++;
            return  new BinOpNode('/', e3, e4);
        }
        return randomExpression(maxHeight);
        
    }

static class BinOpNode extends ExpNode {
        char op;  // the operator, which must be '+', '-', '*', or '/'
        ExpNode left, right;  // the expression trees for the left and right operands.
        BinOpNode(char op, ExpNode left, ExpNode right) {
            if (op != '+' && op != '-' && op != '*' && op != '/')
                throw new IllegalArgumentException("'" + op + "' is not a legal operator.");
            this.op = op;
            this.left = left;
            this.right = right;
        }
        double value(double x) {
            double a = left.value(x);  // value of the left operand expression tree
            double b = right.value(x); // value of the right operand expression tree
            switch (op) {
            case '+': return a + b;
            case '-': return a - b;
            case '*': return a * b;
            default:  return a / b;
            }
        }
        public String toString() {
            return "(" + left.toString() + op + right.toString() + ")";
        }
    }
      
    
        }
static ExpNode随机表达式(int maxHeight){
ExpNode e1=新的二进制节点('+',新的VariableNode(),新的ConstNode(maxHeight));
ExpNode e2=新的二进制节点('*',新的ConstNode(maxHeight),新的VariableNode());
ExpNode e3=新的双节点('*',e1,e2);
ExpNode e4=新的二进制节点('-',e1,新的ConstNode(-3));
如果(最大高度<0){
返回null;
}
如果(maxHeight==0){
返回新的二进制节点('-',e1,e2);
} 
如果(最大高度>0){
maxHeight++;
返回新的二进制节点('/',e3,e4);
}
返回随机表达式(最大高度);
}
静态类BinOpNode扩展了ExpNode{
char op;//运算符,必须是“+”、“-”、“*”或“/”
ExpNode left,right;//左右操作数的表达式树。
BinOpNode(字符操作、ExpNode左、ExpNode右){
如果(op!='+'&&op!='-'&&op!='*'&&op!='/'))
抛出新的IllegalArgumentException(““+op+””不是合法运算符。”);
this.op=op;
this.left=左;
这个。右=右;
}
双值(双x){
double a=left.value(x);//左操作数表达式树的值
double b=right.value(x);//右操作数表达式树的值
开关(op){
案例“+”:返回a+b;
案例'-':返回a-b;
案例“*”:返回a*b;
默认:返回a/b;
}
}
公共字符串toString(){
返回“(“+left.toString()+op+right.toString()+”)”;
}
}
}

您能更详细地描述一下“让方法工作时遇到问题”的实际含义吗?你对代码的哪一部分有问题?什么东西没有按你预期的那样工作?代码没有编译吗?运行时是否出现错误?当你运行它时,你会得到错误的结果吗?嗨@OhGodspides,这个程序是令人信服的,没有错误。我收到的反馈是,我必须回顾“return randomExpression(maxHeight);”这一行,因为它不会被执行,而且我还在返回语句之前增加maxHeight(一个参数)的值,这没有任何效果。在这种情况下,我不太知道您希望得到更多的信息。这是一个非常具体的反馈,已经指出了你的确切错误。如果您想进一步了解为什么不执行该返回语句:请查看包含上述返回语句的If语句,如
If(maxHeight<0)
,并尝试考虑一个值
maxHeight
,该值将导致代码到达行
return randomExpression(maxHeight))谢谢,@ohgodspeiders。我将one if语句更改为for语句。它似乎工作正常。