⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fibonacci.java

📁 tutorial solutions for java programing
💻 JAVA
字号:
// Exercise 3.10
public class Fibonacci { //saved as "Fibonacci.java"
	public static void main (String args[]) {
		int n = 3; // The index n for F(n), starting from n=3.
		int fn; // F(n) to be computed.
		int fnMinus1 = 1; // F(n‐1), init to F(2).
		int fnMinus2 = 1; // F(n‐2), init to F(1).
		int nMax = 20; // Maximum n, inclusive.
		int sum = 2; // initialize the sum to 2, F(0)+F(1)==2.
		double average;
		
		System.out.println("The first " + nMax + " Fibonacci numbers are:");
		//print out the first two initialized Fibonacci number.
		System.out.print(fnMinus1 + " ");
		System.out.print(fnMinus2 + " ");
		
		while (n <= nMax) {	
		// Compute F(n), print it and add to sum.
			fn = fnMinus1 + fnMinus2;
			System.out.print(fn + " ");
			sum += fn;
		// Adjust the index n and shift the numbers.
			n++;
			fnMinus2 = fnMinus1;
			fnMinus1 = fn;
		}
		System.out.println();
		// compute and display the average (=sum/nMax)
		average = (double)sum / nMax;
		System.out.println("The average is " + average);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -