Reputation: 738
when I run the for
loop towards the bottom of this code I get an infinite loop that outputs the output in the if
statement every single time with i
being incremented each time and doesn't stop. I'm pretty sure it's a small problem that I just can't put my finger on...
import java.util.Scanner;
public class HW2johnson_pp1 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.printf("Please enter the species with the higher" +
" population first\n");
System.out.printf("Enter the name of your first species ");
String Species1 = keyboard.nextLine();
System.out.printf("Enter the species' population: ");
int Pop1 = keyboard.nextInt();
System.out.printf("Enter the species' growth rate: ");
int Growth1 = keyboard.nextInt();
System.out.printf("Enter the name of your second species: ");
String Species2 = keyboard.nextLine();
System.out.printf("Enter the species' population: ");
int Pop2 = keyboard.nextInt();
System.out.printf("Enter the species' growth rate: ");
int Growth2 = keyboard.nextInt();
if (Pop2 > Pop1) {
System.out.printf("The first population must be higher. \n");
System.exit(0);
}
Species input1 = new Species();
input1.name = Species1;
input1.population = Pop1;
input1.growthRate = Growth1;
Species input2 = new Species();
input2.name = Species2;
input2.population = Pop2;
input2.growthRate = Growth2;
if ((input1.predictPopulation(1) - input2.predictPopulation(1)) <=
(input1.predictPopulation(2) - input2.predictPopulation(2))){
System.out.printf(Species2 + " will never out-populate " +
Species1 + "\n");
}
else {
for (int i = 0; input2.predictPopulation(i) <=
input1.predictPopulation(i); i++) {
if (input2.predictPopulation(i) == input1.predictPopulation(i)) {
System.out.printf(" will out-populate \n");
}
}
}
}
}
public int predictPopulation(int years)
{
int result = 0;
double populationAmount = population;
int count = years;
while ((count > 0) && (populationAmount > 0))
{
populationAmount = (populationAmount +
(growthRate / 100) * populationAmount);
count--;
}
if (populationAmount > 0)
result = (int)populationAmount;
return result;
}
Upvotes: 0
Views: 250
Reputation: 183582
This:
if (Pop2 > Pop1) {
Species2 = Species1;
Pop2 = Pop1;
Growth1 = Growth2;
}
does not make sense. Apparently what you intend is to "swap" the values for the two populations, but instead, you just cause the two populations to be identical, so that for any value of years
, predictPopulation(years)
will be the same for both objects.
Edited to add: This test:
if ((input1.predictPopulation(1) - input2.predictPopulation(1)) <
(input1.predictPopulation(2) - input2.predictPopulation(2)))
is not a valid way to see whether species 2 will eventually overtake species 1. If species 2 has a greater proportional growth rate (growthRate
), then it will eventually overtake species 1, even if during the first few years, it has a lower absolute growth rate. (This only tangentially relates to your infinite loop — Dampsquid points out that you wouldn't have had the infinite loop if you'd had <=
instead of <
— but it's a problem you need to fix.)
Upvotes: 3
Reputation: 1281
Your issue is here
for (int i = 0; input2.predictPopulation(i) <= input1.predictPopulation(i); i++) {
The population of input1 is never becoming less then input2 this has to do with one of the following:
input1 is getting a parameter value that would not allow it's "population" to become less then input2 (check ranges of inputs to make sure this can occur, maybe something like making sure that input2's growth rate is > input1)
There is something wrong with the class itself that is forcing input1 to always be equal to or greater then input2.
EDIT: since you have posted the code for predictPopulation it looks like it is number one. Make sure that your growth rate for input2 is always greater then input1.
Upvotes: 1