Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Fibonacci Generators and the loop commands.

Brandon Choate
Greenhorn
Posts: 14
Hello!

I'm trying to create a program that uses the Fibonacci series.
You are supposed to be able to input a number into the program, then use a while or for loop to keep adding the number for the fibonaci generator.

.... and here is the other half.

This is what I have so far. Where should I go from here? the "while" command doesn't recognize anything from the second program.

On a related note, how would I be able to get the program to loop back around and ask for another number after it is finished with the calculations?

Larry Frissell
Ranch Hand
Posts: 82
2
1) Your while loop needs a boolean condition, like while(x>5).
2) you are not doing anything with the int return from nextNumber()
3) it is not clear what you are doing with n ( other than printing it)

Try this, but this will only print " while loop ", this will get you started, but you will need to think about where you want the program to go from here.

Varun Goenka
Ranch Hand
Posts: 37
let me suggets a far simpler algorithm.
Declare 2 variables t1 and t2 with values 0 and 1 respectively.
Then within the loop write this
{
t3=t1+t2;
t2=t3;
t1=t2;
}

Let the loop work till accepted value is less than t3.

Simple isnt it. .
No sweat.

Brandon Choate
Greenhorn
Posts: 14
Thanks! That helped quite alot!

My new program now looks like:

If I were to input the number 3, it'd output "fib(3) = ".
Problem is, I can't get the the last number to output after the equals sign (in this case, it'd be 2).

">

Larry Frissell
Ranch Hand
Posts: 82
2
Have you tried running the code? I ask because there appears to be some issues you have not addressed. "n" is declare as a double, then as a String within the main method. Since the double n is not initialized, I do not think the loop will end

I would suggest taking this one step at a time, first looking at what your while loop is doing and using some System.out.print() statements to see if the loop is doing what you expect.