java program

来源:百度知道 编辑:UC知道 时间:2024/06/07 09:58:48
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.
请直接给出答案,再附上代码

public void display(){
int firstO=1;
int firstT=2;
int current;
int count=3;
Ststem.out.println("The first two is+firstO+ firstT);
while(true){
current=firstO+firstT;
System.out.println("第"+count+"个数是+current);
firstO=firstT;
firstT=current;
}
}

int sum = 1,t = 0;

for(int l = 1,m = 1,n = 2;l < = 10;l++){
sum = sum + n;
t = n;n += m;m = t;

}