for循环实现斐波拉切数列的前N项,N由用户输入。

public static void main(String[] args) {
        System.out.println("请输入位数:");
        Scanner input = new Scanner(System.in);
        int N = input.nextInt();
        int first = 1, second = 1, next;
        System.out.print("1" + "1");
        for (int i = 3; i <= N; i++) {
            next = first + second;
            first = second;
            second = next;
            System.out.print(next);
        }
    }

猜你喜欢

转载自www.cnblogs.com/taoist123/p/10136354.html