简体   繁体   English

固定利率数组

[英]array for Fixed rate

i have to find Population for pond . 我必须找到池塘的人口。 it has starting population when user input n after that use input Fixed rate for the population finally user input for how many gerbartion to calculate the current population . 当用户输入n之后,它使用固定比率作为人口,当用户输入n时,它具有起始人口。最后,用户输入多少等级来计算当前人口。 Im stuck at array of calculation how write the code it .can someone help me.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);

You pretty much have the solution in your comment; 您的评论中几乎包含了解决方案。 you even have the extra variable required to keep the population as is when dividing in your formula. 在公式中进行除法运算时,您甚至还具有保持population不变所需的额外变量。

for (int i = 0; i < iFGeneration; i++) {
    iFCurrentPopulation += ((iFCurrentPopulation * iRate) / iStartingPopulation);
}

Note: this will produce an int (obviously) as the result - where as if you want decimal places etc, you should use a double . 注意:这将产生一个int (很明显)作为结果-好像要小数位等,应该使用double

Example: 例:

Initial population = 100 初始人口= 100

Fixed Rate = 80 固定利率= 80

Generation = 3 代= 3

Will produce 583 (as an int ), but in reality the figure would be 583.2 (as a double ). 将产生583 (作为int ),但实际上该数字将为583.2 (作为double )。

See it online! 在线查看!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM