Fibonacci numbers filled



  • I thought it was a simple task, even googled the right code... But in the console, there is a set of a given amount filled with units. What's the mistake?

    public class Test {
    
    public static void main(String[] args) {
    
      prMass(fibMass(20));
    
    }
    
    public static int[] fibMass(int x)
    {
        int m[]=new int[x];
        for(int i=0;i<x;i++)
        {
            if(m[i]<2)
                m[i]=1;
            else
                m[i]=m[i-1]+m[i-2];
        }
    
        return m;
    }
    public static void prMass(int[] x)
    {   int z=x.length;
        for(int i=0;i<z;i++)
        {
            System.out.print(x[i]+" ");
        }
        System.out.println();
    }
    

    }



  • for(int i=0;i<x;i++) {
        if(m[i]<2)
             m[i]=1;
        else
             m[i]=m[i-1]+m[i-2];
    }
    

    You're for every element of the mass, if it's less than two, you're giving equal weight. 1♪ And move to the next element.

    Since the mass is originally initiated by zeros, it is expected that you receive a body of units.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2