无法在循环中更改变量的值

时间:2015-04-22 22:24:23

标签: java loops for-loop jgrasp

我之前从未做过循环,不得不参与一个项目。 这就是我所拥有的:

import java.util.Scanner;
public class Population
{
    public static void main (String [] args)
    {
        Scanner kb = new Scanner (System.in);
        int dailyPopInc=-1;
        System.out.print("What is the starting number of organisms? ");
        int population = kb.nextInt();
        if (population>1){System.out.print("What is the daily population increase as a percentage? ");
            dailyPopInc= kb.nextInt();}
        else System.out.println("Error");
        int daysMultiplied=0;
        if (dailyPopInc>=0){System.out.print("How many days will they multiply? ");
            daysMultiplied= kb.nextInt();}
        int k=0;
        for (k=1;k<daysMultiplied;k++){
            population= population + population*(dailyPopInc/100);
            System.out.println("The the amount of population on day "+k+" is " + population);
        }
    }
}

我不断得到类似的东西 &#34;第1天的人口数是89&#34;它只会改变日期值。

人口永远不会改变。有人可以告诉我我的错误吗?

1 个答案:

答案 0 :(得分:0)

修改这些行:

 double population = kb.nextInt();
 population= population + population*(dailyPopInc/100.0);

因为dailyPopInc / 100作为整数始终为0。