Java 整数在应该增加时不增加

Java 整数在应该增加时不增加,java,input,java.util.scanner,Java,Input,Java.util.scanner,编写一个程序来预测生物种群的大小。节目应该问 对于生物的起始数量,其日均种群数量增加(以百分比表示), 它们将增加的天数。例如,一个总体可能从两个开始 生物的平均日增长率为50%,并将允许在未来几年内繁殖 七天。程序应该使用循环来显示每天的人口数量。 输入验证:人口的起始大小不接受小于2的数字。做 不接受日均人口增长的负数。不要接受数字 小于1表示它们将相乘的天数 我的问题是每天都没有增加。 我的示例输入是100个生物体,增加50%,3天 我的输出是 第1天:100 第2天:100 第3天:10

编写一个程序来预测生物种群的大小。节目应该问 对于生物的起始数量,其日均种群数量增加(以百分比表示), 它们将增加的天数。例如,一个总体可能从两个开始 生物的平均日增长率为50%,并将允许在未来几年内繁殖 七天。程序应该使用循环来显示每天的人口数量。 输入验证:人口的起始大小不接受小于2的数字。做 不接受日均人口增长的负数。不要接受数字 小于1表示它们将相乘的天数

我的问题是每天都没有增加。 我的示例输入是100个生物体,增加50%,3天

我的输出是 第1天:100 第2天:100 第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);
       } 

    }

}
import java.util.Scanner;
公共阶层人口{
公共静态void main(字符串参数[]){
扫描仪=新扫描仪(System.in);
System.out.println(“请输入生物体的数量”);
字符串inputOrganisms=scanner.nextLine();
int numoforganism=Integer.parseInt(inputoorganism);
System.out.println(“请输入生物每日数量
增加(以百分比表示)”;
String inputPopIncr=scanner.nextLine();
double-popregress=Integer.parseInt(inputPopIncr)/100;
System.out.println(“请输入生物体繁殖的天数”);
String inputNumOfDays=scanner.nextLine();
int numOfDays=Integer.parseInt(inputNumOfDays);
对于(int i=1;i
您的问题:

在for循环中,您应该有

numOfOrganisms += numOfOrganisms * popIncrease;
这背后的原因是,您需要将人口增长添加到现有的数字中

您所做的操作会导致错误,因为您只需要在语法行中有一个等于。未读取第二个等于(+=),因为它无效


干杯

numoforbias=numoforbias+=
。哎呀。@BoristheSpider您忘了在旁注中指出,
(numoforbias*=popEncrease)
,您的代码并没有解决输入验证问题,输入验证至少要占到作业分数的三分之一。
Integer.parseInt(inputPopIncr)/100
将为零,除非您输入的值至少为100(或-100)。然后,它可能没有你期望的值。除以
100.0
@JonnyHenly哈哈,是的。我知道,还没有开始。请阅读。请解释一下代码的作用以及它如何解决OPs问题。@TimothyTruckle谢谢你的输入,我编辑了我的答案以符合标准。谢谢!第二个等于(+=)没有被读取,因为它是无效的。-是,逻辑上无效,但语法上不完全无效。@JonnyHenly是的course@JonnyHenly哦,是的,对不起,这就是我的意思。编辑以修复。