This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

The Fibonacci numbers Fn are defined as follows. F0 is 1, F1 is 1, and Fi+2 = Fi + Fi+1

i = 0, 1, 2, ... . In other words, each number is the sum of the previous two numbers. The first few Fibonacci numbers are 1, 1, 2, 3, 5, and 8. One place where these numbers occur is as certain population growth rates. If a population has no deaths, then the series shows the size of the population after each time period. It takes an organism two time periods to mature to reproducing age, and then the organism reproduces once every time period. The formula applies most straightforwardly to asexual reproduction at a rate of one offspring per time period. In any event, the green crud population grows at this rate and has a time period of five days. Hence, if a green crud population starts out as 10 pounds of crud, then in five days there is still 10 pounds of crud; in ten days there is 20 pounds of crud, in fifteen days 30 pounds, in twenty days 50 pounds, and so forth. Write a program that takes both the initial size of a green crud population (in pounds) and a number of days as input, and outputs the number of pounds of green crud after that many days. Assume that the population size is the same for four days and then increases every fifth day. Your program should allow the user to repeat this calculation as often as desired.

Here is my code:

I'd really appreciate some guidance on this, I lost myself in the for statement and I'm not sure what to do next.

Edit by mw: Added closing code tag. [ October 04, 2007: Message edited by: marc weber ]

First, for(int i=5; i==5; i++) will execute the loop body only once (because i==5 is only true the first time). I think you want this loop to iterate for the number of days that the user inputs.

Next, "size" references a String, so size + size is a concatenated String. Instead, I think you want to work with the numeric value, "initialSize."

In doing this, I don't think you don't want x + x. Instead, you want x + (previous x). So you might consider introducing another variable to hold the previous value.

See what you come up with based on that.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org

um - yes. Your for loop starts at 5 and ends at 5. I assume that this is where you want your calculation; but by putting your prompt in here as well, it looks like you're mixing up the calculation with the prompt for new starting values.

I don't think your problem is with the Java code - it might go better if you figured out first how you'd do it "by hand". For instance, first you ask the user for input; then you calculate his answer; then you give him his answer; then (separately) you ask him if he wants you to do another calculation.

Then the calculation belongs in the for loop that you've started; but right now you aren't using the user's time period input in the loop at all.

I sometimes find it's easier if I'm stuck to get the logic right using pencil and paper, and then worry about how to translate that into code.