Reputation: 73
So I have everything working except that once I enter the input required I get input like this:
1 5.0
2 6.0
3 7.0
4 8.0
I don't know what I'm doing wrong as it seems its not increasing in the right increments based on the growthRate that I input which was 50. Also can't get the organism number to increase according to the following day. Any suggestions?
//Purpose of program to predict population of organisms
import java.util.Scanner;
public class Population {
public static void main(String[] args) {
double growthRate = -1;
int population = 0;
int days = -1;
double popResult = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("\nEnter the starting number of organisms:");
population = keyboard.nextInt();
while (population < 2) {
System.out.println("\nError!! Please re-enter number of organisms.");
population = keyboard.nextInt();
}
System.out.println("\nEnter rate of growth as percentage:");
growthRate = keyboard.nextInt() / 100;
while (growthRate < 0) {
System.out.println("\nError!! Growth rate must be a positive number. Please re-enter.");
growthRate = keyboard.nextInt();
}
System.out.println("\nEnter number of days organisms will grow:");
days = keyboard.nextInt();
while (days < 0) {
System.out.println("\nError!! Number of days cannot be less than 1. Please re-enter.");
days = keyboard.nextInt();
}
System.out.println("Days" + "\t" + "Organisms");
System.out.println("------------------");
popResult = population;
growthRate = growthRate / 100;
for (int numberOfDays = 1; numberOfDays < days; numberOfDays++) {
System.out.println(numberOfDays + "\t" + popResult);
popResult = (popResult * growthRate) + popResult;
}
}
}
Upvotes: 0
Views: 46
Reputation: 2122
You are taking input for growthRate as Integer format in line
growthRate=keyboard.nextInt()/100;
If it is less than 0 then you take input without dividing by 100 as
growthRate=keyboard.nextInt();
and finally you are again dividing growthRate as growthRate=growthRate/100;
So you have to take input outside the while loop only as growthRate=keyboard.nextInt();
Modified code
import java.util.Scanner;
public class Population
{
public static void main(String[] args)
{
double growthRate=-1;
int population=0;
int days=-1;
double popResult=0;
Scanner keyboard=new Scanner(System.in);
System.out.println("\nEnter the starting number of organisms:");
population=keyboard.nextInt();
while(population<2)
{
System.out.println("\nError!! Please re-enter number of organisms.");
population=keyboard.nextInt();
}
System.out.println("\nEnter rate of growth as percentage:");
growthRate=keyboard.nextInt();
while(growthRate<0)
{
System.out.println("\nError!! Growth rate must be a positive number. Please re-enter.");
growthRate=keyboard.nextInt();
}
System.out.println("\nEnter number of days organisms will grow:");
days=keyboard.nextInt();
while(days<0)
{
System.out.println("\nError!! Number of days cannot be less than 1. Please re-enter.");
days=keyboard.nextInt();
}
System.out.println("Days" + "\t" + "Organisms");
System.out.println("------------------");
popResult=population;
growthRate=growthRate/100;
for(int numberOfDays=1; numberOfDays<days; numberOfDays++)
{
System.out.println(numberOfDays + "\t" + popResult);
popResult=(popResult * growthRate) + popResult;
}}}
Upvotes: 1