1

我编写了一个程序来在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;
}
}
4

3 回答 3

8

print(不要println)每个数字都在同一行。每当您的计数器i是 10 的倍数时,就开始一个新行。

for (int i = 1; i <= number; i++) {
    System.out.print(fibonacciLoop(i) + " ");
    if (i % 10 == 0)
        System.out.println();
}
于 2013-08-01T11:07:20.030 回答
2
if(i % 10 == 0)
{
    System.out.println();
}

或者,如果你想变得花哨。

System.out.print(fibonacciLoop(i) + " " + (i % 10 == 0? "\n":""));
于 2013-08-01T11:08:01.050 回答
0

请注意,由于以下 for 循环,您的斐波那契计算不起作用:

for (int x = 1; x <= number; x++){
        return BigInteger.valueOf(x);
    }

以下将按照您的要求进行:

import java.math.BigInteger;
import java.util.Scanner;
import java.util.LinkedList;


public class Test {

    Test(int number) {
        System.out.println("\n\nFibonacci series upto " + number + " numbers : ");
        LinkedList<BigInteger> list = new LinkedList<BigInteger>();
        for (int i = 1; i <= number; i++) {
            list.add(fibonacciLoop(i));
            if(list.size() == 10) {
                printFibo(list);
                list = new LinkedList<BigInteger>();
            }
        }
        if(!list.isEmpty()) printFibo(list);
    }

    public static void main(String args[]) {
        System.out.print("Enter number upto which Fibonacci series to print: ");
        new Test(new Scanner(System.in).nextInt());
    }

    private void printFibo(LinkedList<BigInteger> list) {
        for(BigInteger fiboNumber : list) {
             System.out.print(fiboNumber + " ");
        }
        System.out.println("");
    }

    public BigInteger fibonacciLoop(int number) {
        if (number == 1 || number == 2) {
            return BigInteger.valueOf(1);
        }
        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;
    }
}
于 2013-08-01T11:21:43.283 回答