有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
package com.hello.cn;
import java.util.Scanner;
public class SumNew {
public static double sum(int times) {
double numerator=2;
double denominator=1;
double a=0;
for(int i=1;i<=times;i++) {
numerator=numerator+denominator;
denominator=numerator-denominator;
a+=(double)numerator/denominator;
}
return a;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输出一个数:");
int times=sc.nextInt();
System.out.println(sum(times));
}
}