public class TwentiethFractionSum {
/**
* 一分數序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數列`這裏寫代碼片`的前20項之和
* @param n 項數
*/
private static void numberSum(int n){
int x = 2;
int y = 1;
int t = 0;
double sum = 0;
DecimalFormat df = new DecimalFormat("#0.000000");
for(int i=1;i<=20;i++){
sum += (double)x/y;
t = x;
x = y+x;
y = t;
System.out.println("第"+i+"項之和爲:"+df.format(sum));
}
}
public static void main(String[] args) {
numberSum(20);
}
}