我希望这个程序采用一个数组列表“max”,即用户创建的大小。然后程序使用Fibonacci序列存储arraylist中的所有Fibonacci数,这些数字小于由用户输入创建的arraylist“max”。
ExpressionWrapper(db.ForceDate(F(a))-db.ForceDate(F(b)), output_field=DurationField())
答案 0 :(得分:1)
修改并简化了Fibonacci
函数的逻辑。已添加评论以帮助您了解更改。
public static ArrayList<Integer> Fibonacci (Integer max) { //Instead of 'Max' use 'max'
ArrayList<Integer> A = new ArrayList<Integer>();
//Initialize value of n0, n1 and n2
int n0=0;
int n1=1;
int n2=1;
//Handling the base conditions
if(max == 0) return A;
if(max == 1) {
A.add(n0);
return A;
}
//Add first 2 elements in the array
A.add(n0);
A.add(n1);
//A 'while' loop will be more suitable to what you want to achieve
while(n2 < max) {
A.add(n2); //Instead of printing the values, add them into ArrayList A
n0=n1;
n1=n2;
n2 = n1 + n0;
} //Add a closing bracket for the 'for' loop
return A;
}
答案 1 :(得分:0)
试试这段代码。
import java.util.*;
import javax.swing.*;
public class FibonacciArrayList {
public static ArrayList<Integer> Fibonacci (int Max){
ArrayList<Integer> A = new ArrayList<Integer>();
int n0=0;
int n1=1;
int n2;
if(Max==0){
}
else if(Max==1)
{
System.out.println(n0);
A.add(n0);
}
else if(Max==2)
{
System.out.println(n0);
System.out.println(n1);
A.add(n0);
A.add(n1);
}
else{
System.out.println(n0);
System.out.println(n1);
A.add(n0);
A.add(n1);
for(int i= 2; i <=Max; i++){
n2= n1 + n0;
A.add(n2);
System.out.println(n2);
n0=n1;
n1=n2;
}
}
return A;
}
public static void main (String[] arg){
int max;
String Title = "Fibonacci ArryList";
String data = JOptionPane.showInputDialog(null, "Enter the upper bound", Title, 1);
max = Integer.parseInt(data);
ArrayList<Integer> A = Fibonacci(max);
System.out.println("There are " + A.size()+ " Fibonacci numbers less than "+max);
}
}