Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

fibonacci loop repeadly ask the user a question unless user enter "

$
0
0
I need help creating a loop.The loop will ask the user for a number in the Fibonacci series. if the user enter 3... the program will print out 2, the program will keep asking the user to enter a new number unless the user enters 0



Scanner i = new Scanner (System.in);

System.out.println("Given a number n, this program will return the N-th Fibonacci number in an interger type.\n");

do
{

int number = 0;

System.out.print ("Please input your number: ");
number = i.nextInt();

System.out.printf (" The %d-th is fibinocci number is %.0f\n",number, fibcalic(number));

}
while (number != 0);

}

} // end main method

public static int fibcalic(int number)// method
{
int max = number;
int[] fibarray = new int [max];// set the array to int max is still int

fibarray [0] = 0;
fibarray [1] = 1;

for (int a=2; a < max; a++)
{
fibarray[a] = fibarray[a -1] + fibarray[a -2];
}
return fibarray[number];
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>