user2970463
user2970463

Reputation: 43

Updating Total in Java Loop

I am having trouble updating the total for each time the loop runs. I've been using the values 2, 50 and 5 to check my work. The total should read: 3, 4.5, 6.75, 10.125, 15.1875, but all I can get is the first total.

Any help is very much appreciated! Thanks!!

Here's my code:

import java.util.Scanner;

public class Population 
{

public static void main(String[] args) 
{
    int startingOrganisms;
    int dailyPopulation;
    int daysMultiply;
    double dailyTotal = 0;
    double dailyTotal2 = 0;
    double dailyTotal3 = 0;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Starting # of Organisms:");
    startingOrganisms = keyboard.nextInt();

    System.out.println("Daily Population Increase(%):");
    dailyPopulation = keyboard.nextInt();

    System.out.println("# of Days They'll Multiply:");
    daysMultiply = keyboard.nextInt();

    for (int i = 1; i<=daysMultiply; i++)
    {
        dailyTotal = startingOrganisms*dailyPopulation*.01;
        dailyTotal2 = startingOrganisms + dailyTotal;
        System.out.println("Population for day " + i + " = " + dailyTotal2);
    }

}

}

Upvotes: 2

Views: 560

Answers (3)

StoopidDonut
StoopidDonut

Reputation: 8617

As per your logic, and the results it looks like you want to change startingOrganisms value as you loop through your for loop.

Given your input values as:2, 50 and 5 and the result to be: 3, 4.5, 6.75, 10.125, 15.1875, a rough guess would be startingOrganisms should be assigned the value of dailyTotal2

First change would to change the data type of startingOrganisms.

double startingOrganisms;

Next would be to change its value within the for loop:

for (int i = 1; i <= daysMultiply; i++) {
            dailyTotal = startingOrganisms * dailyPopulation * .01;
            dailyTotal2 = startingOrganisms + dailyTotal;
            startingOrganisms = dailyTotal2;
            System.out.println("Population for day " + i + " = " + dailyTotal2);
        }

OR better, treat startingOrganisms as your dailyTotal2 counter which holds the updated value and increments:

for (int i = 1; i <= daysMultiply; i++) {
            dailyTotal = startingOrganisms * dailyPopulation * .01;
            startingOrganisms = startingOrganisms + dailyTotal;
            //startingOrganisms = dailyTotal2
            System.out.println("Population for day " + i + " = " + startingOrganisms);
        }

The result in both the above cases you get here is what you expect:

Starting # of Organisms:
2
Daily Population Increase(%):
50
# of Days They'll Multiply:
5
Population for day 1 = 3.0
Population for day 2 = 4.5
Population for day 3 = 6.75
Population for day 4 = 10.125
Population for day 5 = 15.1875

Upvotes: 1

cdkMoose
cdkMoose

Reputation: 1636

Each day you are calculating based on the original population, not the current population. Try adjusting the calculation to use the current population times the growth factor:

growth = dailyTotal*dailyPopulation*.01;
dailyTotal dailyTotal + growth;

or in one line

dailyTotal = dailyTotal + (dailyTotal*dailyPopulation*.01);

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53525

You can get only the first total cause you're calculating it only once!

You should read the input from the user and calculate in a loop:

while(daysMultiply != -1){ // when the user wants to quit he'll enter -1, for example
    daysMultiply = keyboard.nextInt();
    dailyTotal = 0;
    dailyTotal2 = 0;
    for (int i = 1; i<=daysMultiply; i++)
    {
        dailyTotal = startingOrganisms*dailyPopulation*.01;
        dailyTotal2 = startingOrganisms + dailyTotal;
        System.out.println("Population for day " + i + " = " + dailyTotal2);
    }
}

Upvotes: 2

Related Questions