无法继续循环

时间:2019-03-05 05:04:10

标签: java

我正在尝试使此代码循环,因此一旦我在主while循环中运行,它就会重新开始并再次请求输入程序。我在代码底部没有输入任何注释,由于某种原因,当程序到达此行时,它将终止。

enter image description here 我正在寻找的输出图像。

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

    }
}

1 个答案:

答案 0 :(得分:0)

嗨,请找到修改后的工作代码。

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

        }
    }