While loop and overall calculations

I'm taking a Java course in Community College and we have an assignment. The assignment wants us to ask the user for input on Pythons, calculate their age and the sum of their eggs over their lifetime.

It then asks us to take the final totals of each sumOfEggs and print it for the user to see.

System.out.println(pythonID + " will lay a total of " + sumOfEggs + " eggs over her remaining lifetime of 20 years.");

I have a few issues going on that I've been wracking my brain on. I've looked over my textbook, my previous assignments, and my PPTs but I can't figure it out.

When I go for a second loop, it doesn't start fresh, it keeps adding 35 to the sumOfEggs & previousYearEggs.

The other issue is that I can't figure out how to save the number presented in the output

System.out.println(pythonID + " will lay a total of " + sumOfEggs + " eggs over her remaining lifetime of 20 years.");

The following is my entire program:

import java.util.Scanner;
import java.io.*;

public class camelCase
{
   public static void main(String[] args)
   {

      String runProgram = " ";      //declare Run Program
      String pythonID = " ";        //declare Python ID
      int pythonAge = 0;            //declrea the Python's Age
      int previousYearEggs = 0;     //declare Previous Years Eggs
      int currentYearEggs = 0;      //declare current year's eggs
      int sumOfEggs = 0;            //declare sum of eggs
      int years = 0;                //declare years
      int maxAge = 20;              //declare Age Maximum
      int minAge = 1;               //declare Age Minimum
      int overallTotal = 0;

      //create a scanner class for keyboard input
      Scanner keyboard = new Scanner(System.in);

      //Inform the user of this program's purpose
      System.out.println("This is the Python Snake Eggstimator Program.");
      System.out.println("It estimates the number of eggs that a female python will produce over a lifetime.");

      //prompt the user for input
      System.out.println("Please enter HISS if you want to run the program or STOP to quit.");
      runProgram = keyboard.nextLine();
      runProgram = runProgram.toLowerCase();

      //while loop activated when prompted to run program
      while (runProgram.equals("hiss"))
      {

         System.out.println("Please enter the Python ID.");
         pythonID = keyboard.next();

            //initialize the currentYearEggs accumulator
            currentYearEggs = 0;

            //initialize the maxAge accumulator
            maxAge = 20;

            //Prompt user to input the age of the Python
            System.out.println("Enter the Age of the Python in Years.");
            pythonAge = keyboard.nextInt();

            //Invalid Response while loop
            while (pythonAge < minAge || pythonAge > maxAge)
            {
               System.out.println("Invalid Age: Please enter a number between 1 and 20.");
               pythonAge = keyboard.nextInt();
            }

            //Table Header
            System.out.printf("%-5s%20s%20s%20s\n", "Year", "Previous Year Eggs", "Current Year Eggs", "Sum of all Eggs");

            //for loop to calculate the input 
            for (int i = pythonAge; i <= maxAge; i++)
            {
               //initialize currentYearEggs
               currentYearEggs =  35;

               //Calculation for Sum Of All Eggs
               sumOfEggs = sumOfEggs + currentYearEggs;

               //Output data
               System.out.printf("%5d%20d%20d%20d\n", i, previousYearEggs, currentYearEggs, sumOfEggs);

               //calculate the Previous Years eggs
               previousYearEggs = sumOfEggs;


            }//end for


         //output dialogue for user giving details about their input and calculations
         //prompt to restart the program
         System.out.println(pythonID + " will lay a total of " + sumOfEggs + " eggs over her remaining lifetime of 20 years.");
         System.out.println("Enter HISS if you want to run the program or STOP to quit.");
         runProgram = keyboard.next();
         runProgram = runProgram.toLowerCase();

      }//end runProgram while


      System.out.println("The sum of all eggs for all Pythons processed is "); //+ overallTotal);

   }//main

}//class

I appreciate any help in advance!

1 answer

  • answered 2018-10-11 20:01 syntagma

    When I go for a second loop, it doesn't start fresh, it keeps adding 35 to the sumOfEggs & previousYearEggs.

    You initialize sumOfEggs at the beginning of your main method: int sumOfEggs = 0; but you don't set it to 0 later, so it can only work for the first Python.

    The other issue is that I can't figure out how to save the number presented in the output

    You simply need to add the current sumOfEggs to your overallTotal:

    //for loop to calculate the input 
    sumOfEggs = 0;
    for(int i = pythonAge; i <= maxAge; i++) {
        //initialize currentYearEggs
        currentYearEggs = 35;
    
        //Calculation for Sum Of All Eggs
        sumOfEggs = sumOfEggs + currentYearEggs;
    
        //Output data
        System.out.printf("%5d%20d%20d%20d\n", i, previousYearEggs, currentYearEggs, sumOfEggs);
    
        //calculate the Previous Years eggs
        previousYearEggs = sumOfEggs;
    
    
    }//end for
    overallTotal += sumOfEggs;