今天是计算并打印斐波那契数列的第n个数字,n由用户指定。感觉代码不多,但并不好想
package com.itblackdog.Test; import java.util.Scanner; //计算并打印斐波那契数列的第n个数字,n由用户指定 public class Fibonacci { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("输入要找序列的第几个数:"); int a = scanner.nextInt(); System.out.println(Fbnp(a)); } public static int Fbnp(int a){ if (a == 1||a == 2){ return 1; }else { return Fbnp(a-1)+Fbnp(a-2); } } }