0
采纳
递归
class Atairs
{
private static $numStep = 1;
public function goStep($num)
{
if ($num > 1) {
$step = mt_rand(1, 2);
$laststep = $num - $step;
echo "第" . self::$numStep . "次走了" . $step . "步,还有" . $laststep . "步<br>";
static::$numStep++;
self::goStep($laststep);
} else if ($num == 1) {
echo "第" . self::$numStep . "次走了" . $num . "步,还有0步<br>";
self::goStep(0);
} else {
echo "走完了";
}
}
}
$obj = new Atairs();
$obj->goStep(50);