我必须找到池塘的人口。它在用户输入n之后具有起始人口使用输入人口的固定率最终用户输入用于计算当前人口数量的gerbartion。我坚持计算数组怎么写代码呢。有人帮助我.Thax
import java.util.Scanner;
public class population {
private static final int TARGET = 6000;
public static void main(String[] args) {
int iStartingPopulation,iFGeneration,iRate;
int[] iGrowthRate = new int[10];
Scanner inConsole = new Scanner (System.in);
// Read the starting population of the fish pond
System.out.println(" Enter the starting population of the Fish pond");
iStartingPopulation= inConsole.nextInt();
// Ask whether the growth rate is fixed or variable
System.out.println(" Enter F for fixed growth rate of the pond, V for Variable ");
char option = inConsole.next().charAt(0);
if ( option ==('f') || option==('F')){
//Read the fixed Growth Rate
System.out.println("Enter the fixed growth rate for genration:");
iRate = inConsole.nextInt();
//Read the fixed Growth for how many Generation
System.out.println("Enter How many Generation for:");
iFGeneration= inConsole.nextInt();
int iFCurrentPopulation= iStartingPopulation;
for (int iI =0; iI < iFiGeneration; iI++)
{
iFCurrentPopulation = iFGeneration+( iFCurrentPopulation *iRate /100);
System.out.println("Population :" +""+iFCurrentPopulation);
答案 0 :(得分:0)
你的评论中几乎有你的解决方案;在划分公式时,您甚至需要额外的变量来保持population
。
for (int i = 0; i < iFGeneration; i++) {
iFCurrentPopulation += ((iFCurrentPopulation * iRate) / iStartingPopulation);
}
注意:这会产生一个int
(显然)作为结果 - 就像你想要小数位等一样,你应该使用double
。
示例:的
初始人口= 100
固定利率= 80
代= 3
将生成583
(作为int
),但实际上该数字为583.2
(作为double
)。