【发布时间】:2018-10-11 19:48:42
【问题描述】:
我正在社区学院学习 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。
另一个问题是我不知道如何保存输出中显示的数字
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
提前感谢您的帮助!
【问题讨论】:
-
那么,要理解这个问题,是否必须每年添加 35 个鸡蛋,直到蟒蛇达到最大年龄?
-
嗨@mayhem,没错。对不起,如果我没有澄清。在 20 岁时,应该在 println 中输出并存储以添加到末尾,其中显示“处理的所有蟒蛇的所有鸡蛋的总和是”。每个循环都应该重新开始,但我无法弄清楚。谢谢!
-
我建议在开始第二个 for 循环之前初始化
sumOfEggs,否则如果有人想再次使用计算器,请不要重置sumOfEggs -
类似
sumOfEggs = 0;for (int i = pythonAge; i <= maxAge; i++)... -
它不会重新开始,因为您将
sumOfEggs初始化一次:int sumOfEggs = 0;并且您没有将其设置为0,每次您将鸡蛋相加时都应该这样做新蟒蛇。
标签: java for-loop input while-loop calculation