Java 使用斐波那契序列的基本阵列列表

Java 使用斐波那契序列的基本阵列列表,java,arraylist,Java,Arraylist,我希望这个程序获取一个数组列表“max”,其大小由用户创建。然后,程序使用斐波那契序列在arraylist中存储小于由用户输入创建的arraylist“max”的所有斐波那契数 import java.util.*; import javax.swing.*; public class FibonacciArrayList { public static ArrayList<Integer> Fibonacci (Integer Max){ ArrayList&l

我希望这个程序获取一个数组列表“max”,其大小由用户创建。然后,程序使用斐波那契序列在arraylist中存储小于由用户输入创建的arraylist“max”的所有斐波那契数

import java.util.*;
import javax.swing.*;

 public class FibonacciArrayList {

 public static ArrayList<Integer> Fibonacci (Integer Max){

     ArrayList<Integer> A = new ArrayList<Integer>();
   int n0;
   int n1;
   int n2;

   for(int i= 0; i = max; i++){
     n2= n1 + n0;
     system.out.println(n2);
     n0=n1;
     n1=n2;

  return A;
}


  public static void main (String[] arg){
    Integer max;
    String Title = "Fibonacci ArryList";
    String data = JOptionPane.showInputDialog(null, "Enter the upper bound",    Title, 1);
  max = new Integer(data);
    ArrayList<Integer> A = Fibonacci(max);
    System.out.println("There are " + A.size()+ " Fibonacci numbers less than "+max);

    }

}
import java.util.*;
导入javax.swing.*;
公共类fibonacciraylist{
公共静态数组列表Fibonacci(最大整数){
ArrayList A=新的ArrayList();
int n0;
int-n1;
int n2;
对于(int i=0;i=max;i++){
n2=n1+n0;
系统输出打印ln(n2);
n0=n1;
n1=n2;
返回A;
}
公共静态void main(字符串[]arg){
整数最大值;
字符串Title=“Fibonacci ArryList”;
String data=JOptionPane.showInputDialog(null,“输入上限”,标题,1);
max=新整数(数据);
ArrayList A=斐波那契(最大值);
System.out.println(“存在小于“+max”的“+A.size()+”斐波那契数);
}
}
试试这段代码

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

    }

}
import java.util.*;
导入javax.swing.*;
公共类fibonacciraylist{
公共静态数组列表Fibonacci(int Max){
ArrayList A=新的ArrayList();
int n0=0;
int n1=1;
int n2;
如果(最大==0){
}
否则如果(最大==1)
{
系统输出打印项次(n0);
A.添加(n0);
}
否则如果(最大==2)
{
系统输出打印项次(n0);
系统输出打印ln(n1);
A.添加(n0);
A.添加(n1);
}
否则{
系统输出打印项次(n0);
系统输出打印ln(n1);
A.添加(n0);
A.添加(n1);

for(int i=2;i修改并简化了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;
}
public static ArrayList Fibonacci(整数max){//使用“max”代替“max”
ArrayList A=新的ArrayList();
//初始化n0、n1和n2的值
int n0=0;
int n1=1;
int n2=1;
//处理基本条件
如果(max==0),则返回A;
如果(最大==1){
A.添加(n0);
返回A;
}
//在数组中添加前2个元素
A.添加(n0);
A.添加(n1);
//“while”循环更适合您想要实现的目标
而(n2<最大值){
A.add(n2);//不打印值,而是将它们添加到ArrayList A中
n0=n1;
n1=n2;
n2=n1+n0;
}//为“for”循环添加结束括号
返回A;
}

设置n0=0,n1=1…现在它们甚至没有值。另外,它是System.out.println(n2),带有大写字母s。您需要使用a.add(0)将数字添加到数组列表中。看起来您正在将最大整数添加到数组列表中,但听起来您只想添加小于最大值的整数。