[英]array for Fixed rate
我必须找到池塘的人口。 当用户输入n之后,它使用固定比率作为人口,当用户输入n时,它具有起始人口。最后,用户输入多少等级来计算当前人口。 我陷入了计算数组的困境,如何编写代码。有人可以帮助我吗。
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);
您的评论中几乎包含了解决方案。 在公式中进行除法运算时,您甚至还具有保持population
不变所需的额外变量。
for (int i = 0; i < iFGeneration; i++) {
iFCurrentPopulation += ((iFCurrentPopulation * iRate) / iStartingPopulation);
}
注意:这将产生一个int
(很明显)作为结果-好像要小数位等,应该使用double
。
例:
初始人口= 100
固定利率= 80
代= 3
将产生583
(作为int
),但实际上该数字将为583.2
(作为double
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.