• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Fibonacci Generators and the loop commands.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic