我编写了一个程序来在java中显示斐波那契数列,但现在我希望序列中的数字以十位显示,即一行上的前十个斐波那契数,然后是下一行的下十个等等......我一直有真正的问题,使这成为可能。
这是程序:
import java.util.Scanner;
import java.math.BigInteger;
class Fibonacci {
public static void main(String args[]) {
System.out.print("Enter number upto which Fibonacci series to print: ");
int number = new Scanner(System.in).nextInt();
System.out.println("\n\nFibonacci series upto " + number + " numbers : ");
for (int i = 1; i <= number; i++) {
System.out.println(fibonacciLoop(i) + " ");
}
}
public static BigInteger fibonacciLoop(int number) {
if (number == 1 || number == 2) {
return BigInteger.valueOf(1);
}
for (int x = 1; x <= number; x++){
return BigInteger.valueOf(x);
}
BigInteger fibonacci = BigInteger.valueOf(1);
BigInteger fibo1 = BigInteger.valueOf(1);
BigInteger fibo2 = BigInteger.valueOf(1);
for (int i = 3; i <= number; i++) {
fibonacci = fibo1.add(fibo2);
fibo1 = fibo2;
fibo2 = fibonacci;
}
return fibonacci;
}
}