Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions