2011-10-22 43 views
0

我們必須創建一個斐波那契系統。你能告訴我我做錯了什麼嗎?它在while循環下給了我一個錯誤,但我確定這是我構造變量的方式。斐波納契家庭作業 - java

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

爲什麼你不告訴_us_你有什麼錯誤? –

+5

拼寫斐波那契。 – akappa

+1

'new int [0]'是一個只有0項的數組,這肯定是錯誤的。 – Vlad

回答

4

幾個問題:

  1. new int [0]意味着一個空數組,它不是你想要的 。
  2. 在第一次循環執行時X值爲0,所以Fn [X-1]爲Fn [-1],其中 將導致ArrayOutOfBoundException。

您能否更清楚地瞭解您遇到的錯誤?

1

您的sumFn數組聲明的長度爲0.因此,無論何時嘗試向其中添加任何元素,您都將獲得ArrayOutOfBoundException

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]); 
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(); 
    } 
} 

至於你的錯誤,請閱讀其他答案。他們的建議很好。 :)

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(); 
     } 
}