public static void main(String[] args) { Rabbit r = new Rabbit(); r.chuanQi(); System.out.println("-----------------------\n仅以此程序献给疯狂的兔子"); } }
个人感觉用数组不太好 数组的边界问题始终比较麻烦 常用的解法应该是这样吧 public class TEST{
static int f1 = 1; static int f2 = 1; static int f3 = 0; public static void main(String [] args){ for(int i = 3;i<15;i++){ f3 = f1 + f2; f1 = f2 ; f2 = f3 ; System.out.println("第" + i + "个月的兔子数为" + f2);
这里可以不这样写在写个for()循环嵌套进去
lz你可以慢慢想想
public long getCount(long month) {
if (month < 3)
return 1;
else {
return getCount(month - 1) + getCount(month - 2);
}
}
i[j] = i[j-1] + i[j-2];
这里会报错吧;for中j起始为3则有i[3] = i[2] + i[1];
但你初始化i[0] = 1; i[1] = 1; 所以 你懂的
忘了插入源代码了 sorry各位的眼睛受苦了
public class Rabbit {
int[] i;
public void chuanQi() {
i = new int[15];
i[0] = 1;
i[1] = 1;
for (int j=2; j<14; j++) {
i[j] = i[j-1] + i[j-2];
System.out.println("第" + (j-1) + "个月有兔子" + i[j] + "对--" + 2 * i[j] + "只");
}
}
public static void main(String[] args) {
Rabbit r = new Rabbit();
r.chuanQi();
System.out.println("-----------------------\n仅以此程序献给疯狂的兔子");
}
}
public class TEST{
static int f1 = 1;
static int f2 = 1;
static int f3 = 0;
public static void main(String [] args){
for(int i = 3;i<15;i++){
f3 = f1 + f2;
f1 = f2 ;
f2 = f3 ;
System.out.println("第" + i + "个月的兔子数为" + f2);
}
}
}
然后循环你要的月份 sum1=sum1+sum2;第三个月
sum2=sum1+sum2;第四个月
记住每次for循环中不是i++而是i+=2