java - coding errors causing endless loop
Posted
by
Daniel Key
on Stack Overflow
See other posts from Stack Overflow
or by Daniel Key
Published on 2012-10-26T22:52:40Z
Indexed on
2012/10/26
23:00 UTC
Read the original article
Hit count: 304
Im attempting to write a program that takes a population's birthrate and deathrate and loops the annual population until it either reaches 0 or doubles. My problem it that it continuously loops an endless amount of illegible numbers and i cant fix it. please help.
//*****************************************
//This program displays loop statements
//Written by: Daniel Kellogg
//Last Edited: 9/28/12
//****************************************
import java.util.Scanner;
public class Hwk6 {
public static void main (String[] args) {
int currentYear, currentPopulation;
double birthRate, deathRate;
Scanner stdin = new Scanner(System.in);
System.out.println("\nPopulation Estimator\n");
System.out.println("Enter Year");
currentYear = stdin.nextInt();
System.out.println("Enter Current Population");
currentPopulation = stdin.nextInt();
System.out.println("Enter Birthrate of Population");
birthRate = stdin.nextDouble();
System.out.println("Enter Deathrate of Population");
deathRate = stdin.nextDouble();
int counter = currentPopulation;
System.out.println("Population: ");
while (currentPopulation != -1)
while (counter < currentPopulation * 2) {
System.out.print(counter + " ");
counter = counter + (int)(counter * birthRate - counter * deathRate);
}
System.exit(0);
}
}
© Stack Overflow or respective owner