Java实现间隔解析int

时间:2017-03-08 10:17:45

标签: java

如何实现interval parse int以增加线性整数数组的增量? 并将其归还

public class Sequence { 
    /*Generate an array representing a linear sequence of N values specified by the start value and the interval example,interval(6, 2, 3) would produce {2,5,8,11,14,17}*/
    public int[] Linear(int N, int start, int interval){
        int interval = Integer.parseInt(args[0]);
        for (int i = 1; i<= N; i++);
        i = i + interval;


    }

    //Generate an array of the first N values of the Fibonacci sequence (1,1,2,3,5,8,13,21,...). Assume N > 2
    public static int[] fibonacci(int N){
        int[] fibo = new int [N + 1];
        fibo[0] = 1;
        fibo[1] = 1;
        for (int i = 2; i<= N; i++)
            fibo[i]= fibo[i-1] + fibo[i-2];
        //  int[] x = fibo;
        return fibo;

    } 

    public static void main(String[] args) { 

        int N = Integer.parseInt(args[0]);      
        //fibonacci(N);



        for (int i = 1; i <= N; i++ )

            System.out.println(fibonacci(i));


    }
}

1 个答案:

答案 0 :(得分:1)

这就是Linear方法的样子:

public int[] Linear(int N, int start, int interval){

    // First declare the array.
    int[] linearArray = new int[N];

    // Declare something to hold the next value
    // The first value will be "start"

    int value = start;

    // Then, iterate over a for loop 
    for (int i = 0; i< N; i++){
        // assign the current value to the current indexed element in the array
        linearArray[i] = value;

        // compute next value
        value = value + interval;
    }

    // Finally, return your array
    return linearArray;
}