1

所以这是我的第一个递归函数(我希望!),我不确定为什么它不起作用,(又名红线)有什么想法吗?

int myFactorial(int C) { //underlined, expects ";"

    int n = Integer.parseInt(objectsChooseField.getText());
    int r = Integer.parseInt(chooseFromField.getText());

    if (C == 1){
        return 1; //underlined, cannot return value from method whose result type is void
    }
    return (C*(myFactorial(n/(r(n-r))))); //underlined
}
4

4 回答 4

10

这里:r(n-r)

r不是函数,而是局部变量int

你的意思是r * (n - r)

于 2013-05-11T16:38:52.570 回答
2

回避语句不应该是:

return ( C * myFactorial ( C - 1 ) );
于 2013-05-11T16:42:44.647 回答
0

你的方程没有说明任何递归。教师可以通过递归计算。

public int MyMethod() {
int n = Integer.parseInt(objectsChooseField.getText());
int r = Integer.parseInt(chooseFromField.getText());
int result = C( n, r );
}

public int C( int n, int r ) {
  int res = faculty( n ) / ( faculty( r ) * ( n - r ));
  return res;
}

//--- Computing the faculty itself could be done by recursion :-)
public int faculty( n ) {
  if ( n > 1 )
    return n * faculty( n - 1 );
  return 1;
}
于 2013-05-11T17:16:35.090 回答
0

好的。所以你有 ActionPerformed 方法。输入如下代码:

private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
    int n = Integer.parseInt(objectsChooseField.getText());
    int r = Integer.parseInt(chooseFromField.getText());
    int result = faculty( n ) / ( faculty( r ) * ( n - r ));
    //--- Output result to somewhere
}

以及教师计算方法本身:

/** Computes the faculty of n */
public int faculty( n ) {
  if ( n > 1 )
    return n * faculty( n - 1 );
  return 1;
}
于 2013-05-12T16:21:27.753 回答