public class Demo2 {
/**
* @param args
*/
public static void main(String[] args) {
int i =2;
int[] ss = new int[20];
ss[0]=1;
ss[1]=1;
System.out.print(ss[0]+" "+ss[1]+" ");
while(i<20){
ss[i] = ss[i-1]+ss[i-2];
System.out.print(ss[i]+" ");
i++;
}
}
}
上面一个用了数组,下面这个不用数组(数组感觉还是跟递归一个样):
public class Demo2 {
/**
* @param args
*/
public static void main(String[] args) {
int i =2;
int m =1;
int n =1;
int next;
System.out.print(m+" "+n+" ");
while(i<20){
next = m +n;
m=n;
n=next;
System.out.print(next+" ");
i++;
}
}
}
import java.io.*; public class Test30007 { public static void main(输出时你没转换为int型吧 你可以强制转换下(int) convernt int(string) .