我們必須創建一個斐波那契系統。你能告訴我我做錯了什麼嗎?它在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++;
}
}
}
爲什麼你不告訴_us_你有什麼錯誤? –
拼寫斐波那契。 – akappa
'new int [0]'是一個只有0項的數組,這肯定是錯誤的。 – Vlad