所以这是我的第一个递归函数(我希望!)并且我不确定为什么它不起作用(又名红线)任何想法?
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
}
答案 0 :(得分:10)
此处:r(n-r)
。
r
不是函数,而是局部变量int
。
您的意思是r * (n - r)
吗?
答案 1 :(得分:2)
递归语句不应该是:
return ( C * myFactorial ( C - 1 ) );
答案 2 :(得分: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;
}
答案 3 :(得分: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;
}