-1

Write a program that will predict the size of a population of organisms. The program should ask for the starting number of organisms, their average 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 multiply for seven 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 issue is that each day is not increasing. my example input is 100 organisms, 50 percent increase, and 3 days

My output is day 1: 100 day 2: 100 day 3: 100

import java.util.Scanner;

 public class Population {

    public static void main(String args[]) {
           Scanner scanner = new Scanner( System.in );

        System.out.println("Please input the number of organisms");
          String inputOrganisms = scanner.nextLine();
        int numOfOrganisms = Integer.parseInt(inputOrganisms);

          System.out.println("Please input the organisms daily population 
  increase (as a percent)");
          String inputPopIncr = scanner.nextLine();
        double popIncrease = Integer.parseInt(inputPopIncr) /100;


        System.out.println("Please input the number of days the organisms will multiply");
          String inputNumOfDays = scanner.nextLine();
        int numOfDays = Integer.parseInt(inputNumOfDays);

       for (int i = 1; i < numOfDays+1; i++) {
           numOfOrganisms = numOfOrganisms += (numOfOrganisms *= popIncrease);
           System.out.println("Day " + i + ": " + numOfOrganisms);
       } 

    }

}
Cory
  • 35
  • 7

1 Answers1

1

Your problem:

In the for loop you should have

numOfOrganisms += numOfOrganisms * popIncrease;

The reason behind this is because you need to add the population increase to the existent number.

What you are doing causes an error, as you need to have only one equals in a syntax line. The second equals (the +=) isn’t being read as it is invalid.

Cheers!

Jacob B.
  • 423
  • 3
  • 12
  • 2
    Please read [How do I write a good answer?](/help/search?q=how+to+answer). Please [add](/help/editing) an explanation what the code does and how it solves the OPs problem. – Timothy Truckle Jan 08 '18 at 18:48
  • @TimothyTruckle thanks for that input, I edited my answer to fit the criteria. Thank you! – Jacob B. Jan 08 '18 at 18:52
  • *The second equals (the +=) isn’t being read as it is invalid.* - Yes logically invalid but not quite syntactically invalid. – Jonny Henly Jan 08 '18 at 18:54
  • @JonnyHenly Yes of course – Jacob B. Jan 08 '18 at 18:54
  • @JonnyHenly oh yes, sorry that’s what I meant. Edited to fix. – Jacob B. Jan 08 '18 at 18:57
  • @JacobB. Thank you mate. – Cory Jan 08 '18 at 19:15
  • "What you are doing causes an error, as you need to have only one equals in a syntax line. The second equals (the +=) isn’t being read as it is invalid." This is total poppycock. You can have many assignments and compound assignments in a single statement. The problem is that the semantics of the operators is such that you are effectively adding zero to the variable. – Andy Turner Jan 08 '18 at 19:15
  • @JacobB.the last sentence is wrong - the second equal is being read, it is valid and being invali results in an error not just not being read – user85421 Jan 09 '18 at 15:50