Java-斐波拉契数列

目标

写一个程序打印斐波拉契 (Fibonacci) 数列,数列的长度可以由输入指定。


代码

import java.io.IOException;
import java.util.Scanner;


public class Fibonacci {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		int a = 1;
		int b = 1;
		int m,count = 1;
		System.out.print("请输入长度:");
		Scanner s = new Scanner(System.in);
        int t = s.nextInt(); 
//		System.out.println("t:"+t);
		
		System.out.print(" 1"+" ");
		do{
			m = a + b;
			int temp = 0;
			temp = m;//交换所得结果
			a = b;
			b = temp;
			System.out.print(a+" ");
			count ++;
		}while(count <= t -1);
	
		
		
		

	}

}

你可能感兴趣的:(Java)