【问题标题】:mixture of recursive and loop递归和循环的混合
【发布时间】:2013-09-26 05:38:00
【问题描述】:

所以我遇到了递归问题。

编写一个程序来预测生物种群的大小。该程序应询问生物的起始数量、它们的平均每日种群增加量(以百分比表示)以及它们将繁殖的天数。 例如,一个种群可能从两个生物开始,平均每天增加 50%,并且将被允许繁殖 7 天。程序应该使用循环来显示每天的人口规模。 输入验证:

  • 不接受小于 2 的数字作为总体的起始规模。
  • 不接受负数表示平均每日人口增长。
  • 不要接受小于 1 的数字作为它们将相乘的天数。
  • 确保您的程序使用递归方法而不是循环来计算生物体的数量。
  • 请在您正在完成的任何方法的开头记录前置条件和后置条件。如果不存在,则每个都声明不存在。

我们必须编写一个displayPopulation 方法来显示每天的人口规模。然后,我们必须编写递归方法来计算大小,然后我们有 main 用于输入验证。

现在,我有了主方法和递归方法。我想不通的是如何让循环显示每天的大小? 以下是我的递归方法:

private static double showPopulation(int dayNum, int days, double organisms, double dailyIncrease){

  if(dayNum==days)
      return organisms;
   else 

        return showPopulation(dayNum+1, days, organisms + organisms*(dailyIncrease/100), dailyIncrease);
}

我知道这会给我所有天的总和。我不知道如何存储每个值并使其以不同的方法循环打印。我真的迷路了。我需要一些帮助,我将不胜感激。

【问题讨论】:

  • 你有没有为 displayPopulation 尝试过什么?如果没有,你会如何口头解决?这有时可以帮助我确定可行的算法。
  • @Rabia Khan 如果你找到任何答案。帮助已满,无法将其标记为已接受。

标签: java loops recursion


【解决方案1】:

在任何 for 或 while 循环中编写一个方法,并调用此方法并传递方法所期望的值。并打印结果。

注意-我不是在编写代码,因为那应该由您完成。首先您尝试编写代码,然后如果您得到任何输出,那么我们可以检查您的代码并尝试更正它。

【讨论】:

    【解决方案2】:
    private static double PrintPopulation(int numDays, int days,
            double organism, double increase) {
        if (numDays == days)
            return organism;
        else {
            double recentPopulation = organism + (organism * increase / 100);
            System.out.println("Population for day:" + numDays + " is: "
                    + recentPopulation);
            return PrintPopulation(numDays + 1, days, recentPopulation,
                    increase);
        }
    
    }
    

    这应该可以解决您的问题。 days 是总天数

    【讨论】:

      【解决方案3】:
      private static double showPopulation(int dayNum, int days, double organisms, double dailyIncrease){         
          if(dayNum==days)
              return organisms;
          else{ 
              //put your print statement here  population in `days` is  `organisms`
              return showPopulation(dayNum+1, days, organisms + organisms*(dailyIncrease/100),            dailyIncrease); 
          }
      
      }
      

      【讨论】:

        【解决方案4】:
        private static double showPopulation(int dayNum, int days, double organisms, double dailyIncrease)
        {
             if(dayNum==days)
                  return organisms;
               else 
            {
                System.out.println("Population for day"+(dayNum+1)+" is "+(organisms+organisms*(dailyIncrease/100)));
                return showPopulation(dayNum+1, days, organisms + organisms*(dailyIncrease/100), dailyIncrease);
            }
        
            }
        }
        

        【讨论】:

          【解决方案5】:

          您当前的程序在给定日期返回生物体。如果你想要从第 1 天到第 n 天的生物,那么你可以使用如下的 for 循环:

          double[] organismsArr = new double[days];
          for(int i=1;i<=days;i++)
          organisms[i-1] = showPopulation(i, days, organisms + organisms*(dailyIncrease/100), dailyIncrease);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-01-13
            • 2010-10-24
            • 2012-11-18
            • 1970-01-01
            • 1970-01-01
            • 2018-06-27
            • 2017-01-21
            • 2020-06-28
            相关资源
            最近更新 更多