人口计划 [英] Population program

查看:66
本文介绍了人口计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我在Java编程简介中,我遇到了分配问题。家庭作业被称为人口。


人口


编写一个程序,预测一群生物的大小。该计划应询问有机体的起始数量,它们的平均每日人口增长率(百分比)以及它们将增加的天数。例如,人口可能从两种生物开始,平均每日增加50%,并允许繁殖7天。该程序应该使用一个循环来显示每天的人口规模。


输入验证:不接受小于人口起始大小的数字。不接受平均每日人口增长的负数。不要接受一个小于一的数字,它们会增加它们的天数。


这是我到目前为止所做的事情!


import java.util.scanner; //需要扫描仪课程




/ **

此程序演示用户控制的循环< br $>
* /


公共类人口


public static void main(String [] args)

{

int尺寸,

天,

增加,

数字,

Temp;

字符串输入; //保持用户的输入


//为键盘输入创建一个Scanner对象。

扫描仪键盘=新扫描仪(System.in) ;


int startingSize = 0,dailyIncrease = 0,numberOfDays = 0;

int temp = 0;



//接受初始用户输入


while(startingSize< 2)

{

System.out.print("请为人口规模输入大于1的数字:");

startingSize = keyboardInput.nextInt(System.in);

}


while(dailyIncrease< 0)

{

System.out.print("为种群大小增加输入一个正整数:);

dailyIncrease = keyboardInput.nextInt(System.in);

}


//对numberOfDays执行相同的while循环作为正数> 0


temp = startingSize;

dailyIncrease = dailyIncrease + 1;


for(int i = 0; i< numberOfDays; i ++)

{

System.out.println(" Day" + i);

System.out.println(" Number" + temp);

temp = temp * dailyIncrease;

}

Hello I am in Intro to Java Programming and I am having problems with assignment. The Homework assignment is called Population.

Population

Write a program that will predict the size of a population of organisms. The program should ask for the starting number of organisms, their average daily population increase (as a percentage), and the number of days they will multiply. For example, a population might begin with two organisms, have an average daily increase of 50%, and will be allowed to multiply for 7 days. The program should use a loop to display the size of the population each day.

Input Validation: Do not accept a number less than two for the starting size of the population. Do not accept a negative number for average daily population increase. Do not accept a number less than one for the number of days they will multiply.

Here is what I have done so far!

import java.util.scanner; // Needed for the scanner class




/**
This program demonstrates a user controlled loop
*/

public class Population

public static void main(String[] args)
{
int Size,
Days,
increase,
number,
Temp;
String input; // To Hold the user''s input

//Create a Scanner object for keyboard input.
Scanner Keyboard = new scanner(System.in);


int startingSize = 0, dailyIncrease = 0, numberOfDays = 0;
int temp = 0;


//accept initial user input here

while (startingSize < 2)
{
System.out.print("Please enter a number that is greater than 1 for the population size: ");
startingSize = keyboardInput.nextInt(System.in);
}

while (dailyIncrease < 0)
{
System.out.print("Enter a positive integer for the population size increase: ");
dailyIncrease = keyboardInput.nextInt(System.in);
}

//do the same while loop for numberOfDays as a positive number > 0


temp = startingSize;
dailyIncrease = dailyIncrease + 1;

for(int i = 0; i < numberOfDays; i++)
{
System.out.println("Day " + i);
System.out.println("Number " + temp);
temp = temp * dailyIncrease;
}

推荐答案

到目前为止,一切看起来还算可以。我想指出一些事情:


1)在你的import语句中,你说import java.util.scanner;;但是类名是大写的 - 它应该是import java.util.Scanner;。在创建用于输入的扫描仪时,您犯了类似的错误:扫描仪键盘=新扫描仪(System.in);应该是Scanner Keyboard = new Scanner(System.in);


2)您的问题规范要求输入人口增长的百分比。现在,如果输入1,则人口将增加一倍(因为你将dailyIncrease增加1,并且在每个''day''中,人口乘以dailyIncrease)。您应该重新配置您的程序,将dailyIncrease视为百分比,方法是将其分为100并将其存储为双,然后再添加1。
So far, everything looks fairly OK. I''d like to point out a few things:

1) In your import statement, you say "import java.util.scanner;" but the class name is capitalized - it should be "import java.util.Scanner;". You made a similar mistake when creating your Scanner for input: "Scanner Keyboard = new scanner(System.in);" should be "Scanner Keyboard = new Scanner(System.in);"

2) Your problem specification calls for inputting a percentage for population increase. Right now, if you enter 1, the population will be doubled (because you increase dailyIncrease by 1, and in each ''day'', the population is multiplied by dailyIncrease). You should reconfigure your program to treat dailyIncrease as a percentage by dividng it by 100 and storing it into a double before adding 1 to it.


谢谢!那么还有什么我需要改变或添加的吗?如果你不介意我问?!
Thank you! So is there anything else I needs to change or add? If you don''t mind me asking?!


只有你创建的第一组变量 - int Size,Days,increase,number,Temp;和字符串输入;永远不会被使用,所以你可以删除它们。


为什么不编译并运行它来查看你是否得到了正确的结果?你可以回来收到任何错误或输出错误,但现在我没有看到任何错误。
Only that the first set of variables you create - int Size, Days, increase, number, Temp; and String input; are never used, so you can remove them.

Why don''t you compile and run this to see if you''re getting the correct results? You can come back with any errors you receive or faulty output, but right now I don''t see anything wrong.


这篇关于人口计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆