For Looping Question

Hello All,

This is my first post on these forums... yep... I am a newb. This is my first stab at programming since it is a prerequisite for my bachelor's degree (I work in Information Assurance currently). I have a question regarding looping if someone could give me a quick tip.I was assigned to code a simulation of the Fibonacci numbers for the online course. I am to write a program that takes both the initial size of a green crud population and the number of days as input, and output the number of pounds of green crud after that many days. I have everything working perfectly, however, the thing is the value needs to change on every 5th day. My answers currently calculate every single day. In other words, my output for five days is (for example) 1, 1, 2, 3, 5 - when it should be 1, 1, 1, 1, 2.

Thanks in advance guys,

Much appreciated.

- Andrew

Here is my code:

import java.util.Scanner;

/**

Computes the amount of green crud after a given number of days and a

given starting amount.

*/

public class file {

public static void main(String[] args) {

// Make a Scanner to read data from the console

Scanner console = new Scanner(System.in);

// Read in the initial size of the crud population

System.out.println(

"Enter initial green crud population size (in pounds), ");

System.out.println("or a blank line to quit:");

String size = console.nextLine();

// Stop on blank line

while ((size != null) && (size.length() > 0)) {

double initialSize = Double.parseDouble(size);

// Read in the number of days

System.out.println("Enter the number of (whole) days:");

int days = console.nextInt();

int start;

// start of my code

double x = 0;

double y = initialSize;

double z; // Initialize variables

for (start = 0; start < days; start++) { // Loop

z = y + x; // Next term is sum of previous two

System.out.println(y); // Print Solution

x = y; // First previous becomes previous

y = z; // And current number becomes previous

}

// end of my code

// Start again, giving the user a chance to cancel by entering

// a blank line.

System.out.println();

System.out.println(

"Enter initial green crud population size (in pounds), ");

System.out.println("or a blank line to quit:");

// Skip over the end-of-line for the last number the user entered

console.nextLine();

size = console.nextLine();

}

}

}

[2613 byte] By [Andrew0001a] at [2007-11-27 9:16:43]
# 1
> Information AssuranceI'll bite -- what's that? Will you assure worried people that information is in fact reliable?!
BigDaddyLoveHandlesa at 2007-7-12 22:06:41 > top of Java-index,Java Essentials,New To Java...
# 2

Heh, yes. In IA your client is ALWAYS worried and trying to ensure that it's safe is near impossible. IA is closely related to information security; its just risk management. I send you this document, I need to make sure the document you receive is safe, not hijacked or tampered with. But, of course it's not like I can have a wide open Windows 95 box on a T3 line to send it to you... I need an approved operating system with a sufficient firewall and all security vulnerabilities locked down etc etc.

Andrew0001a at 2007-7-12 22:06:41 > top of Java-index,Java Essentials,New To Java...
# 3

Could give *your* formula in a longer sequence?

I know the F series is:

1 1 2 3 5 8 13 21 ...

You write your series is

1 1 1 1 2 ...

That's not enough to go on! Is it

1 1 1 1 2 2 3 3 5 5 8 8 13 13 21 21 ...

or is it

1 1 1 1 2 3 5 8 13 21 ...

or something else?

BigDaddyLoveHandlesa at 2007-7-12 22:06:41 > top of Java-index,Java Essentials,New To Java...
# 4
I think what he is supposed to have is the fibonacci sequence but instead of increasing everytime it only increases to the next number every 5th day - least that makes the growth of the population seem at least a little more reasonable. ;-P
Aknibbsa at 2007-7-12 22:06:41 > top of Java-index,Java Essentials,New To Java...
# 5

Sure no problem. The size is the same for four days then increases every fifth day. So, for example, it the weight starts at "2 pounds" and spans 20 days the output will be:

2, 2, 2, 2,||2, 2, 2, 2|||4, 4, 4, 4,||||6, 6, 6, 6,||||||10, 10, 10, 10

--1--|||--1-||||--2--|||||--3--|||||5--|||

Because it emulates the Fibonacci formula of 1, 1, 2, 3, 5, 8, etc

My problem is that the loop goes 2, 2, 4, 6, 10 in 5 days; while I coded the formula correctly I need it to output to keep the same number for four days then transition to the next number and so forth. Hope I haven't confused you!

Thanks,

Andrew

Andrew0001a at 2007-7-12 22:06:41 > top of Java-index,Java Essentials,New To Java...
# 6

Well, suppose some code produced the output:

1 2 3 4 5 ....

and you wanted it to instead produce

1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 ...

How could you change it? Why not replace the line where it outputs

one number with a little loop to output that number four times?

BigDaddyLoveHandlesa at 2007-7-12 22:06:41 > top of Java-index,Java Essentials,New To Java...
# 7

Sorry for the delay; I was on travel over the weekend with no internet access. I went to hell's gate this weekend- my girlfriend's parents' house.

Yes, that would definitely work. How would you go about setting up the separate loop while keeping the counter going from the parent loop? I would think the loop would have rules set up to display the variable four times then back to the parent loop.

I really appreciate the help thus far!

Andrew

Andrew0001a at 2007-7-12 22:06:41 > top of Java-index,Java Essentials,New To Java...
# 8

You need to do something like this:

for (parent = initialValue; parent <= breakCondition; parent++){ //outer

for (int i = 0; i < 4; i++){ //inner loop runs 4 times

//Do something

} //end inner loop

} //end outer loop

DarumAa at 2007-7-12 22:06:41 > top of Java-index,Java Essentials,New To Java...