java每日一练
程序员文章站
2022-03-05 10:45:41
...
题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。
public class FenShuQiuHe {
public static void main(String[] args) {
print();
}
public static void print(){
double num = 0.0,sum = 0.0;
double i = 2.0,j = 1.0,temp = 0;
int count = 20;
System.out.println("前"+count+"项之和为:");
while(count>0){
count--;
num = i/j;
sum += num;
temp = j;
j = i;
i = i+temp;
}
System.out.println(sum);
}
}
上一篇: java每日一练