📄 ch6_e6_27.java
字号:
import java.io.*;
import java.util.*;
public class ch6_e6_27
{
static Stack myStack;
static
{
myStack = new Stack();
}
public static void main(String args[])
{
int length = 0;
try
{
System.out.print("请输入菲波那契数列的长度(最大为10):");
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
length = Integer.parseInt(br.readLine());
if(length>10 || length<0)
{
System.out.println("输入数据过大或过小,请输入小于10的正整数。");
return;
}
Fibonacci(length);
}
catch(NumberFormatException nfe)
{
System.out.println(nfe.toString());
System.out.println("格式错误,请输入小于10的正整数");
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
}
}
public static long Fibonacci(int n)
{
long temp;
String str;
str = new String("Fibonacci(" + Integer.toString(n) + ")");
myStack.push(str);
System.out.println(str);
if(n==0 || n==1)
temp = n;
else
temp = Fibonacci(n-1) + Fibonacci(n-2);
System.out.println( (String)(myStack.pop()) + "返回 " + temp);
return temp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -