我将如何实现间隔解析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));
}
}
最佳答案
这是您的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;
}