wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Fibonacci Generators and the loop commands. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Fibonacci Generators and the loop commands. " Watch "Fibonacci Generators and the loop commands. " New topic
Author

Fibonacci Generators and the loop commands.

Brandon Choate
Greenhorn

Joined: Mar 06, 2009
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

Joined: May 16, 2008
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

Joined: Mar 09, 2009
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.


From The Demon,
with love.
Brandon Choate
Greenhorn

Joined: Mar 06, 2009
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

Joined: May 16, 2008
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Fibonacci Generators and the loop commands.
 
Similar Threads
please help
fibonacci program
Fibonacci Sequence Problem
Recursion of Fibonacci Numbers
Storing the last value in a sequence?