Fibonacci家庭作业 - java

时间:2011-10-22 13:57:46

标签: java testing for-loop

我们必须创建一个斐波那契系统。你能告诉我我做错了什么吗?它在while循环下给出了一个错误,但我确信这是我构造变量的方式。

public class Chapter3 {
    public static void main (String args[]){  
        int numFn;//CREATE NUMBER OF FN, SUM OF FN, AND AVERAGE   
        int average[]=new int [0];
        int sumFn []=new int [0];//ARRAY OF SUMFN        
        numFn = 1;//ASSIGN FN AS 1           
        int x = 0;//NUMBERIN SIDE FN ARRAY         
        int Fn []=new int[16];//CREATE FN ARRAY       
        Fn [0]=0;    

        while (numFn <15){
            Fn[x]= Fn[x]-Fn[x-1];//SET THE CURRENT FN NUMBER
            sumFn [x]=sumFn[x]+(sumFn[x-1]+Fn[x]);//SET CURRENT SUMFN NUMBER
            average [x]= sumFn[x]/numFn;

            System.out.println(numFn +"/t" +Fn+"/t" +sumFn+"/t" +average);
            x++;
            numFn++;
        }
    }
}

好吧,我根据你们的建议改变了它,然而第一个输出是1然后是0用于所有内容,使用此代码:

           public class Chapter3 {
        public static void main (String args[]){
          int numFn;//CREATE NUMBER OF FN, SUM OF FN, AND AVERAGE
        int average[]=new int [16];
        int sumFn []=new int [16];//ARRAY OF SUMFN
        numFn = 1;//ASSIGN FN AS 1
        int x = 1;//NUMBERIN SIDE FN ARRAY
        int Fn []=new int[16];//CREATE FN ARRAY
        Fn [0]=0;



    while (numFn <15){
        Fn[x]= Fn[x]-Fn[x-1];//SET THE CURRENT FN NUMBER
        sumFn [x]=sumFn[x]+(sumFn[x-1]+Fn[x]);//SET CURRENT SUMFN NUMBER
        average [x]= sumFn[x]/numFn;

        System.out.println(numFn +"\t" +Fn[x]+"\t" +sumFn[x]+"\t" +average[x]);
        x++;
        numFn++;
    }


}

}

5 个答案:

答案 0 :(得分:4)

几个问题:

  1. new int [0]表示一个空数组,这不是你的     想。
  2. 第一次循环执行时X值为0,因此Fn [X-1]为Fn [-1]     会导致ArrayOutOfBoundException。
  3. 您是否也可以更明确地了解您遇到的错误?

答案 1 :(得分:2)

我认为这就是您所追求的(此代码以1和1开头并打印前20个术语)...

public class Fibonacci {   
    public static void main(String[] args) {     
        int n0 = 1, n1 = 1, n2;    
        System.out.print(n0 + " " + n1 + " ");
        for (int i = 0; i < 18; i++) { // Loop for the next 18 terms
            n2 = n1 + n0; //The next term is the sum of the previous two terms
            System.out.print(n2 + " ");       
            n0 = n1; // The first previous number becomes the second previous number...       
            n1 = n2; // ...and the current number becomes the previous number     
        }     
        System.out.println();
    }
} 

至于你的错误,请阅读其他答案。他们的建议很好。 :)

答案 2 :(得分:1)

您的sumFn数组的长度为0.因此,每当您尝试向其添加任何元素时,您将获得ArrayOutOfBoundException

答案 3 :(得分:0)

代码中存在几个问题,即使在修复导致ArrayIndexOutOfBoundsException的问题之后我怀疑它是否会起作用。对于初学者,您必须以正确的大小初始化您正在使用的阵列:

int average[]=new int [16];
int sumFn []=new int [16];

“x”变量应从1开始:

int x = 1;

此外,还不清楚你想要打印什么,无论如何println()声明应该被修复

System.out.println(numFn +"\t" +Fn[x]+"\t" +sumFn[x] + "\t" +average[x]);

答案 4 :(得分:0)

这对于解决Fibonacci序列练习非常有帮助。但是不打印零,所以我在这里添加...

/*
 * FibonacciSequence.java
 * ----------------------
 * This program displays the values in the Fibonacci sequnece from F0 to F15.
*/

import acm.program.*;

public class FibonacciSequence extends ConsoleProgram{
     public void run() {     
            int n0 = 1;
            int n1 = 1;
            int n2;    
            println("0" + "\n" + n0 + "\n" + n1);
            for (int i = 0; i < 13; i++) { // Loop for the next 18 terms
                n2 = n1 + n0; //The next term is the sum of the previous two terms
                println(n2 + " ");       
                n0 = n1; // The first previous number becomes the second previous number...       
                n1 = n2; // ...and the current number becomes the previous number     
            }     
            println();
        }
}