0

I am trying to get this code to loop so once I run through the main while loop it starts over and asks for the input of the program again. I have a commented out of input at the bottom of the code and for some reason when the program reaches this line it terminates.

enter image description here image of output I'm looking for.

import java.util.Scanner; 

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

        String program="";
        String program2 ="";
        String PythonID;
        int PythonAge = 0;
        int PreviousYr=0;
        int CurrentYr=35;
        int SumEggs=0;
        int GTeggs=0;
        int PythonYr=0;


        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.");

        Scanner input = new Scanner(System.in);
        System.out.println("Please enter HISS if you want run the program STOP to quit");
        program= input.nextLine();

        while (program.equals("HISS"))
        {
            //Ask user to continue program



            //ask for Python ID name
            System.out.println("Enter the Python ID:");
            PythonID= input.nextLine();

            //Python  age
            do 
            {
                System.out.println("Enter the age of the python in years:");
                PythonAge= input.nextInt();
            }
            while(PythonAge <1 || PythonAge >20);

            //setting year to 0    
            PreviousYr=0;
            SumEggs=0;
            //if age is less than 4
            if (PythonAge <4) 
            {
                PythonAge = 4;
            }
            //Calculation egg stuff    
            while (PythonAge <=20)
            {

                SumEggs = PreviousYr + CurrentYr;


                System.out.println("Year " +PythonAge+ " Previous "+PreviousYr + " Current Year " +CurrentYr+ " Sum eggs "+SumEggs);
                PreviousYr= PreviousYr + CurrentYr;
                PythonAge++; 

            }
            //ask for program again
            System.out.println(PythonID+ " will lay a total of " + SumEggs + " over her remaining lifetime of 20 years.");

            System.out.println("Please enter HISS if you want run the program STOP to quit");
            //program= input.nextLine();
            GTeggs=GTeggs + SumEggs;
            System.out.println("The sum of all eggs for all Pythons processed is " + GTeggs);   
        }
        System.out.println("The sum of all eggs for all Pythons processed is " + GTeggs);   

    }
}
Jason
  • 11,258
  • 3
  • 39
  • 46
Joshua
  • 1

1 Answers1

0

Hi please find the modified working code.

import java.util.Scanner;

public class Demo123 {

        public static void main(String[] args)
        {

            String program="";
            String program2 ="";
            String PythonID;
            int PythonAge = 0;
            int PreviousYr=0;
            int CurrentYr=35;
            int SumEggs=0;
            int GTeggs=0;
            int PythonYr=0;


            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.");

            Scanner input = new Scanner(System.in);
            System.out.println("Please enter HISS if you want run the program STOP to quit");
            program= input.nextLine();

            while (program.equals("HISS"))
            {

                //ask for Python ID name
                System.out.println("Enter the Python ID:");
                PythonID= input.nextLine();

                //Python  age
                do
                {
                    System.out.println("Enter the age of the python in years:");
                    PythonAge= input.nextInt();
                    input.nextLine();
                }
                while(PythonAge <1 || PythonAge >20);

                //setting year to 0
                PreviousYr=0;
                SumEggs=0;
                //if age is less than 4
                if (PythonAge <4)
                {
                    PythonAge = 4;
                }
                //Calculation egg stuff
                while (PythonAge <=20)
                {

                    SumEggs = PreviousYr + CurrentYr;


                    System.out.println("Year " +PythonAge+ " Previous "+PreviousYr + " Current Year " +CurrentYr+ " Sum eggs "+SumEggs);
                    PreviousYr= PreviousYr + CurrentYr;
                    PythonAge++;

                }
                //ask for program again
                System.out.println(PythonID+ " will lay a total of " + SumEggs + " over her remaining lifetime of 20 years.");

                System.out.println("Please enter HISS if you want run the program STOP to quit");
                //program= input.nextLine();
                GTeggs=GTeggs + SumEggs;
                System.out.println("The sum of all eggs for all Pythons processed is " + GTeggs);

                System.out.println("Please enter HISS if you want run the program STOP to quit");
                program= input.nextLine();
            }

        }
    }
Chiran K.
  • 406
  • 2
  • 14