Question: This programs asks the user to enter a number n (bigger than 2). It then prints out the first n numbers of the Fibonacci Sequence.

This programs asks the user to enter a number "n" (bigger than 2). It then prints out the first "n" numbers of the Fibonacci Sequence. Each number is the sum of the two previous numbers.

Example: The output for n=11 should look exactly like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

Fix all compile-time and run-time errors.

import java.util.ArrayList; import java.util.List; import java.util.Scanner;

public class FibonacciBuggy { private static Scanner scanner;

public static void main(String[] args) { scanner = new Scanner(System.in); int n = 11; while (n > 2) { System.out.println("Enter a number bigger than 2: "); n = scanner.nextInt(); } printList(getFiboList(n)); }

private static List getFiboList(int n) { List f = new ArrayList(n); f.add(0); f.add(1); int i = 2; while (i < n) { f.add(f.get(i - 1) + f.get(i - 1)); i++; } return f; }

private static void printList(List fiboList) { int i = 2; while (i <= fiboList.size()) { System.out.print(fiboList.get(i)); i++; } System.out.println("..."); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!