无法在我的循环中更改变量的值
Cannot change the variable's value in my loop
我以前从未制作过循环,但必须为项目制作。
这是我拥有的:
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);
}
}
}
我不断收到诸如
"The amount of population on day 1 is 89" 并且它只会更改日期值。
人口永远不会改变。有人可以告诉我我的错误吗?
修改这些行:
double population = kb.nextInt();
population= population + population*(dailyPopInc/100.0);
这是因为dailyPopInc/100作为一个整数总是0。
我以前从未制作过循环,但必须为项目制作。 这是我拥有的:
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);
}
}
}
我不断收到诸如 "The amount of population on day 1 is 89" 并且它只会更改日期值。
人口永远不会改变。有人可以告诉我我的错误吗?
修改这些行:
double population = kb.nextInt();
population= population + population*(dailyPopInc/100.0);
这是因为dailyPopInc/100作为一个整数总是0。