【问题标题】:do-while loop only runs oncedo-while 循环只运行一次
【发布时间】:2018-12-13 14:45:37
【问题描述】:

我正在尝试使用一个类来制作一个简单的 while 循环来获取数字的阶乘。但是,由于某种原因,while 循环仅在运行一次后才返回值。

这是我的代码:

<?php
    class Factorial {
      public function calculate($int){
             $intStep = $int-1;
                     do {
                        $int*=$intStep;
                        $int--;
                        return $int;
                       } while($int>0);    
              if (!is_int($int)){
                 echo "entry is not a number";
                 }
              }
            }

$factorial = new Factorial;
echo $factorial->calculate(5);

【问题讨论】:

  • return $int; 行导致函数返回该值。当您从它返回时,该函数不会继续执行。
  • return 表示退出函数。这就是为什么你的循环只运行一次,因为你正在退出循环中的函数。

标签: php class while-loop do-while factorial


【解决方案1】:

我发现您的代码存在许多问题:

  1. return $int; 在 do while 循环中运行,这意味着它只会运行一次。
  2. 您正在递减 $int 而不是 $intStep
  3. 您正在检查 $int 是否低于零而不是 $intStep

这是修复了所有这些问题的代码,它可以正常工作并返回 15:

class Factorial {

    public function calculate ($int) {
        if (!is_int($int)) {
            echo "entry is not a number";
        }
        $intStep = $int - 1;
        do {
            $int += $intStep;
            $intStep--;
        } while ($intStep > 0);
        return $int;
    }
}

$factorial = new Factorial;
echo $factorial->calculate(5);

3v4l.org demo

【讨论】:

  • 嘿大卫,我试图得到数字的阶乘。这种情况下的输出应该是 120。(5*4*3*2*1)。但是,您向我指出的 3 个问题正是代码出了什么问题,也正是我需要解决的问题。既然你提到了这些问题,我就像“我怎么没注意到这个?多么愚蠢的错误!”我只是花了 2 个小时试图弄清楚这一点......谢谢!!!!!!!
【解决方案2】:

在你的结果准备好之前,你不应该从你的函数中返回。由于你早回来,你不会完成你的计算。

一般来说,如果您学会如何调试,您的生活会更轻松。对于 PHP,最简单的方法是在整个代码中添加 echo 内容。如果您将echo $int 放在循环中,您会更清楚问题所在。

【讨论】:

    【解决方案3】:

    试试这个

     <?php
                class Factorial {
                  public function calculate($num){
              $Factorial = 1;
              $i =1;
                                 do{
                                  $Factorial *= $i;
                                  $i++;
                                }while($i<=$num);
                              return $Factorial;     
    
                          }
                        }
            $factorial = new Factorial;
            echo $factorial->calculate(5);
    ?>
    

    【讨论】:

      【解决方案4】:

      阶乘?也许下面的代码就是你想要的:

      不要忘记负数。

      class Factorial {
      
          public function calculate ($int) {
              if (!is_int($int)) {
                  echo "entry is not a number";
              }
              if ($int == 0 || $int == 1) {
                  $result = 1;
              } else if ($int < 0) {
                  $result = $this->calculate($int + 1) * $int;
              } else {
                  $result = $this->calculate($int - 1) * $int;
              }
              return $result;
          }
      }
      
      $factorial = new Factorial;
      echo $factorial->calculate(-4);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-02
        • 1970-01-01
        • 1970-01-01
        • 2020-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-06
        相关资源
        最近更新 更多