Java实现斐波那契数列,键盘录入个数
package com.homework;
import java.util.Scanner;
public class Homework05 {
public static void main(String[] args) {
//作业2:斐波那契数列:1 1 2 3 5 8 13 21 34 55 89.... 打印前n个值
System.out.println("请输入要打印的斐波那契总个数:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arrNum = new int[n];
System.out.println("斐波那契数列的前" + n + "个数为");
if (n==1){
arrNum[0] = 1;
System.out.println(arrNum[0]);
}else if(n == 2){
arrNum[0] = 1;
arrNum[1] = 1;
System.out.println(arrNum[0] + " " + arrNum[1]);
}else if (n >= 2){
arrNum[0] = 1;
arrNum[1] = 1;
System.out.print(arrNum[0] + " " + arrNum[1] +" ");
for (int i = 2; i < arrNum.length; i++) {
arrNum[i] = arrNum[i - 1] + arrNum[i - 2];
System.out.print(arrNum[i] + " ");
}
}
}
}