Results 1 to 4 of 4

Thread: for loop

  1. #1
    jimJohnson is offline Member
    Join Date
    Jan 2008
    Posts
    22
    Rep Power
    0

    Default for loop

    I am writing a program that will ask the user for the starting number of organisms, their average daily population increase and
    the number of days that will multiply. I have a for loop that shows the results. My problem is when I run the program the calculations are not correct. Day 1 is fine and it works fine but day 2-on does not work. I see what the problem is but I just can't figure out the computations to fix it. In my for loop it reads...

    Java Code:
    		//Show totals for each day
    		for(int count = 1; count <= days; count++)
    		{
    
    			//Calculate the total for each day
    			total =  (total + organisms) * (1 + percentIncrease);
    			
    
    			System.out.println("The total for day " + count + " is " + total);
    
    		}
    So the issue is after I get the total for day 1 It adds organisms integer but I know I need to get that removed somehow as my calculations will keep coming up incorrect.

    I am going to attach the rest of my code as this probably sounds confusing so one of you guys can run it and see the issue. Like I said I knw organisms needs to be removed the calculate statement I just need help how i can go around it.

    Java Code:
    import java.util.Scanner;
    
    /*
    This program will predict the size of a population of organisms.
    The program will ask the user for the starting number of organisms, their average daily population increase and
    the number of days that will multiply.
    */
    
    public class Chapter4Project
    {
    	public static void main(String [] args)
    	{
    		//Declare variables
    		int organisms, days;
    		double percentIncrease;
    		double total = 0.0;
    
    		//Create a scanner object for keyboard input
    		Scanner keyboard = new Scanner(System.in);
    
    		//Ask user how many organisms to start out with
    		System.out.print("How many organisms do you want to start out with: ");
    		organisms = keyboard.nextInt();
    
    		//Make sure the organisms is not less than 2
    		while(organisms < 2)
    		{
    			System.out.println("Organisms cannot be less than 1.");
    			System.out.print("How many organisms do you want to start out with: ");
    			organisms = keyboard.nextInt();
    		}
    
    		//Ask user the daily average percent increase
    		System.out.print("What is the daily average percent increase: ");
    		percentIncrease = keyboard.nextDouble();
    
    		//Make sure the population increase is not less than 0
    		while(percentIncrease < 0)
    		{
    			System.out.println("Daily average percent cannot be a negative number.");
    			System.out.print("What is the daily average percent increase: ");
    			percentIncrease = keyboard.nextDouble();
    		}
    
    		//Calculate the percentIncrease into a percentage
    		percentIncrease /= 100;
    
    		//Ask the user how many days the organisms will multiply
    		System.out.print("How many days will the organisms multiply: ");
    		days = keyboard.nextInt();
    
    		//Make sure the number of days is not less than 1
    		while(days < 1)
    		{
    			System.out.println("The number of days cannot be less than 1.");
    			System.out.print("How many days will the organisms multiply: ");
    			days = keyboard.nextInt();
    		}
    
    		//Show totals for each day
    		for(int count = 1; count <= days; count++)
    		{
    
    			//Calculate the total for each day
    			total =  (organisms) * (1 + percentIncrease);
    			
    
    			System.out.println("The total for day " + count + " is " + totalDay);
    
    		}
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,609
    Rep Power
    5

    Default Re: for loop

    Java Code:
    //Show totals for each day
    for(int count = 1; count <= days; count++)
    {
     
        //Calculate the total for each day
        total =  (total + organisms) * (1 + percentIncrease);
         
     
        System.out.println("The total for day " + count + " is " + total);
     
    }
    I believe what you want to do is initialize total to the number of organisms (out side the loop). Then remove the organisms variable from your calculation within the loop.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: for loop

    The expression at line #65 is 'loop invariant', meaning that the outcome of the expression is the same each time your loop body executes. You most likely don't want that; you probably want something like total+= total*percentIncrease, and initialize total to a meaningful value.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: for loop

    I'm not entirely clear on your requirements but instead of using 'total' you could just assign the calculation on line 65 back to organisms. During the next iteration it would then use this updated value for the calcuations.

    To simplify the code even further you could also use the following.
    Java Code:
    organisms *= 1+percentIncrease;
    If you want to show the increase from one day to the next, you will have to use a temporary variable to hold the previous value and subtract this from the new one.

    Regards.

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  3. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  4. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM
  5. Convert do while loop to for loop
    By sandeeptheviper in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •