我想打印变量count
的最终值,但是当我在countTrees
方法中使用此方法时,它也会打印中间值。我的主要方法无法访问count
。我不想改变返回变量如何将变量的方法应用到主要方法中
public class treecount {
public static void main(String[] args){
countTrees(3);
//System.out.println(countTrees(3));
//System.out.println(count);
}
public static int countTrees(int numKeys) {
//int temp = 0;
int count = 0;
if (numKeys <=1) {
count += 1;
return(1);
}
else {
int sum = 0;
int left, right, root;
for (root=1; root<=numKeys; root++) {
left = countTrees(root-1);
right = countTrees(numKeys - root);
// number of possible trees with this root == left*right
sum += left*right;
count += root*left*right;
}
System.out.println(count);
return(sum);
}
}
}
输出:
3
3
10
输出所需:
10
嘿,伙计们帮我解决这个问题thanx提前
不,这会打印变量总和的值,在这种情况下是5。我想要的是打印值varable count –