Php 创建一个传递正整数的机制,并显示指定值之前(包括指定值)斐波那契级数的所有值 斐波那契:

Php 创建一个传递正整数的机制,并显示指定值之前(包括指定值)斐波那契级数的所有值 斐波那契:,php,html,sequence,fibonacci,Php,Html,Sequence,Fibonacci,这是我的代码,问题是如果我在文本框中输入1,而不是只显示序列中的第一个数字,它会显示前4个数字,因此对于2,它会显示 1 2 3 5 8 13 21 34而不仅仅是1 2,任何ides人员???您的错误在以下几行: <form method="get" action="Index.php"> <fieldset> <label for="powerof">Fibonacci: </label>

这是我的代码,问题是如果我在文本框中输入1,而不是只显示序列中的第一个数字,它会显示前4个数字,因此对于2,它会显示
1 2 3 5 8 13 21 34而不仅仅是1 2,任何ides人员???

您的错误在以下几行:

<form method="get" action="Index.php">
        <fieldset>

            <label for="powerof">Fibonacci: </label>
            <input type="text" name="powerof" value="<?php echo $_GET['powerof']; ?>"/>
            <input type="submit" name='Go' value="Calculate" />
        </fieldset>
    </form>
    <?php
    $message = 'The fibonacci sequence is: <br />1<br />2<br />';
    $powerof = 0;
    $max = 5;
    $temp = $max;

    if (isset($_GET['powerof'])) {
        $powerof = $_GET['powerof'];
    }

    if ($powerof > 100) {
        $powerof = 100;
        $message = 'Sorry, your input was too high. I converted it to the maximum value of 100.<br />The fibonacci sequence is: <br />1<br />2<br />';

    }

    $i = 1;

    for ($i; $i < $powerof; $i++) {
        $max = $max * $temp;
    }

    $x = 1;
    $y = 2;
    $z = $x + $y;

    echo($message);

    while ($z < $max) {

        $z = $x + $y;
        echo($z . "<br />");
        $x = $y;
        $y = $z;
    }
    ?>

while($z
Fibonacci:

这是有效的版本

如果我删除了该部分,那么整个过程就停止了。我已经尝试过了。你可以删除整个“powerof”在PHP5.6中循环并使用
**
,或在任何其他版本中使用
pow
,我可以,但我已经使用了它,所以我不知道该做什么,也不知道该更改什么
$max**$powerof
pow($max,$powerof)
,谢谢,但我已经解决了这个问题。即使如此,您可能仍然应该使用这两个版本中的一个
for ($i; $i < $powerof; $i++) {
    $max = $max * $temp;
}
while ($z < $max) {
while ($z <= $max) {
<label for="powerof">Fibonacci: </label>
<input type="text" name="powerof" value="<?php echo $_GET['powerof']; ?>"/>
<input type="submit" name='Go' value="Calculate" />
</fieldset>
</form>
<?php
$message = 'The fibonacci sequence is: <br />1<br />2<br />';
$powerof = 0;
$max = 10;
$temp = $max;

if (isset($_GET['powerof'])) {
    $powerof = $_GET['powerof'];
}

if ($powerof > 100) {
    $powerof = 100;
    $message = 'Sorry, your input was too high. I converted it to the maximum value of 100.<br />The fibonacci sequence is: <br />1<br />2<br />';

}

$x = 0;
$y = 1;
$z = 0;
$counter = 0;

while ($counter < $powerof) {
    if ($counter <= 1) {
        $z = 1;
    } else {
        $z = $x + $y;
    }
    echo($z."<br />"); 
    $x = $y;
    $y = $z;
    $counter++;
}

?>