我刚开始学习递归。我被要求制作的程序应该使用一个术语作为输入,并输出相应的斐波纳契数。这是我到目前为止,但它显示错误。怎么了?
提前致谢!
private void btnFindNumberActionPerformed(java.awt.event.ActionEvent evt) {
int term = Integer.parseInt(txtInputTerm.getText());
int number = fibonacci(term);
txtOutput.setText("Fibonacci number " + term + " is " + number);
}
public int fibonacci (int term) {
if (term == 1) {
return term;
} else {
int number = fibonacci(term-1) + fibonacci(term-2);
return number;
}
}