Element17
10-10-2013, 11:26 PM
Write a program that will predict the the size of a population of organisms. The program should ask for the starting number of organisms, their daily population increase (as a percentage), and the number of days they will multiply. For example, a population might begin with two organisms, have an average daily increase of 50 percent, and will be allowed to multiplied for 7 days. The program should use a loop to display the size of the population for each day.
Input validation: Do not accept a number less than 2 for the starting size of the population. Do not accept a negative number for average daily population increase. Do not accept a number less than 1 for the number of days they will multiply.
My code...but it doesn't work. I get the same output for everyday. EDIT************Needed to remove the short before the expression!
How do I get it to print just the 48 for day 1?
import java.util.Scanner;
/*Andrew Varnick
10/8/2013
CSCI 1015
Programming Assignment 6
A program that will predict the size of a population of organisms
*/
public class VarnickPass6
{
public static void main(String[] args)
{
int input;
double input2;
int input3;
double temp;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the starting number organisms.");
input = keyboard.nextInt();
while (input < 2)
{
System.out.println("You must enter at least 2 for the starting number.");
System.exit(0);
}
System.out.println("Enter the daily increase.");
input2 = keyboard.nextInt();
while (input2 < 0)
{
System.out.println("You cannot enter a negative number for daily growth.");
System.exit(0);
}
System.out.println("Enter the number of days the organims will multiply.");
input3 = keyboard.nextInt();
while (input3 < 1)
{
System.out.println("You must enter at least 1 day for the number of days.");
System.exit(0);
}
System.out.println("Calculating...");
System.out.println("\n");
System.out.println("Day \t # of Organisms");
System.out.println("----------------------------");
temp = input;
input2 =(input2 / 100) + 1.0; //What I had input2 = (short)((input2 / 100) + 1.0);
for(int i = 1; i <= input3; i++)
{
temp = temp * input2;
System.out.println("\nPopulation increase for day " + i + " is " + temp + "\n");
}
}
}
Input validation: Do not accept a number less than 2 for the starting size of the population. Do not accept a negative number for average daily population increase. Do not accept a number less than 1 for the number of days they will multiply.
My code...but it doesn't work. I get the same output for everyday. EDIT************Needed to remove the short before the expression!
How do I get it to print just the 48 for day 1?
import java.util.Scanner;
/*Andrew Varnick
10/8/2013
CSCI 1015
Programming Assignment 6
A program that will predict the size of a population of organisms
*/
public class VarnickPass6
{
public static void main(String[] args)
{
int input;
double input2;
int input3;
double temp;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the starting number organisms.");
input = keyboard.nextInt();
while (input < 2)
{
System.out.println("You must enter at least 2 for the starting number.");
System.exit(0);
}
System.out.println("Enter the daily increase.");
input2 = keyboard.nextInt();
while (input2 < 0)
{
System.out.println("You cannot enter a negative number for daily growth.");
System.exit(0);
}
System.out.println("Enter the number of days the organims will multiply.");
input3 = keyboard.nextInt();
while (input3 < 1)
{
System.out.println("You must enter at least 1 day for the number of days.");
System.exit(0);
}
System.out.println("Calculating...");
System.out.println("\n");
System.out.println("Day \t # of Organisms");
System.out.println("----------------------------");
temp = input;
input2 =(input2 / 100) + 1.0; //What I had input2 = (short)((input2 / 100) + 1.0);
for(int i = 1; i <= input3; i++)
{
temp = temp * input2;
System.out.println("\nPopulation increase for day " + i + " is " + temp + "\n");
}
}
}