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();
}
}
}

