while循环和整体计算
While loop and overall calculations
我正在社区学院学习 Java 课程,我们有一项作业。该作业要求我们询问用户对 Python 的输入,计算它们的年龄和它们一生中卵子的总和。
然后它要求我们计算每个 sumOfEggs 的最终总数并打印出来供用户查看。
System.out.println(pythonID + " will lay a total of " + sumOfEggs + " eggs over her remaining lifetime of 20 years.");
我有几个问题一直在绞尽脑汁。我看了我的课本,我以前的作业,还有我的PPT,但我想不通。
当我进行第二个循环时,它并没有重新开始,它一直在 sumOfEggs & previousYearEggs.
另一个问题是我不知道如何保存输出中显示的数字
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
在此先感谢您的帮助!
When I go for a second loop, it doesn't start fresh, it keeps adding 35 to the sumOfEggs & previousYearEggs.
你在main方法的开头初始化了sumOfEggs
:int sumOfEggs = 0;
但是你后面没有设置为0
,所以它只能对第一个[=起作用22=]。
The other issue is that I can't figure out how to save the number presented in the output
您只需将当前的 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;
我正在社区学院学习 Java 课程,我们有一项作业。该作业要求我们询问用户对 Python 的输入,计算它们的年龄和它们一生中卵子的总和。
然后它要求我们计算每个 sumOfEggs 的最终总数并打印出来供用户查看。
System.out.println(pythonID + " will lay a total of " + sumOfEggs + " eggs over her remaining lifetime of 20 years.");
我有几个问题一直在绞尽脑汁。我看了我的课本,我以前的作业,还有我的PPT,但我想不通。
当我进行第二个循环时,它并没有重新开始,它一直在 sumOfEggs & previousYearEggs.
另一个问题是我不知道如何保存输出中显示的数字
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
在此先感谢您的帮助!
When I go for a second loop, it doesn't start fresh, it keeps adding 35 to the sumOfEggs & previousYearEggs.
你在main方法的开头初始化了sumOfEggs
:int sumOfEggs = 0;
但是你后面没有设置为0
,所以它只能对第一个[=起作用22=]。
The other issue is that I can't figure out how to save the number presented in the output
您只需将当前的 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;