While循环和整体计算

时间:2018-10-11 19:48:42

标签: java for-loop input while-loop calculation

我正在社区学院上Java课程,我们有一个作业。这项作业要求我们要求用户输入有关Python的信息,计算他们的年龄以及整个生命周期内卵的总和。

然后要求我们获取每个 sumOfEggs 的最终总计,并打印出来以供用户查看。

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

我遇到了一些困扰我的问题。我查看了我的教科书,以前的作业和我的PPT,但我无法弄清楚。

当我进行第二次循环时,它并没有重新开始,而是一直将35添加到 sumOfEggs previousYearEggs < / p>

另一个问题是我不知道如何保存输出中显示的数字

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

以下是我的整个程序:

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

我先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

  

当我进行第二次循环时,它不会重新开始,它会继续将35添加到sumOfEggs和previousYearEggs中。

您可以在主方法sumOfEggs的开始处初始化int sumOfEggs = 0;,但以后不会将其设置为0,因此它仅适用于第一个Python。

  

另一个问题是我不知道如何保存输出中显示的数字

您只需要将当前的sumOfEggs添加到您的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;