TheProgrammerDude TheProgrammerDude - 16 days ago 5
Java Question

Write a program that will ask the user to input a phrase (multiple word sentence), and an integer value

Using the formula from problem #21 on page 145, do the following: Write a program that will output a list of Fibonacci numbers.

Answer
import java.util.Scanner;
public class list
{
    public static void main(String[] args)
    {
        Scanner key = new Scanner(System.in);
        System.out.println("Please enter in a sentence: ");
        String sent = key.nextLine();
        System.out.println("Please enter in a number: ");
        int num = key.nextInt();
        getData(sent.split("\\s+"),num);
    }
    public static void getData(String[] sent,int num)
    {       
        if ((num < 1) || (num > sent.length)){
            System.out.println(sent[sent.length-1]);    
        }
        else{
            System.out.println(sent[num]);
        }

    }
}

This Code works for me

Comments