Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
I';我是PHP新手,我创建了一个For循环,想知道是否有人知道如何将其重新写入while循环?_Php - Fatal编程技术网

I';我是PHP新手,我创建了一个For循环,想知道是否有人知道如何将其重新写入while循环?

I';我是PHP新手,我创建了一个For循环,想知道是否有人知道如何将其重新写入while循环?,php,Php,这是我的密码。它目前按原样工作,但我正在试验循环,希望看到它可以用while循环完成,以及如何完成。有了这段代码,我可以输入2个数字并显示出来,然后指出所有赔率,加上所有的偶数,再加上所有赔率的平方 define ("B","<br/>"); $firstNum = $_POST["firstNum"]; $secondNum = $_POST["secondNum"]; if ($firstNum < $secondNum) { $firstNum = true;

这是我的密码。它目前按原样工作,但我正在试验循环,希望看到它可以用while循环完成,以及如何完成。有了这段代码,我可以输入2个数字并显示出来,然后指出所有赔率,加上所有的偶数,再加上所有赔率的平方

define ("B","<br/>");

$firstNum = $_POST["firstNum"];
$secondNum = $_POST["secondNum"];

if ($firstNum < $secondNum)
{
    $firstNum = true;
}
    elseif ($firstNum >= $secondNum)
    {
        $firstNum = "You didn't listen, dumb dumb!".'<br/><a href="Assignment5form.php">GO BACK</a>';
    }

echo "First Number: ".$firstNum."<br/>"."Second Number: ".$secondNum;

echo B;
echo B;

$numbers = array();
$numbers = range($firstNum, $secondNum);
$length = count($numbers);
$odds = array();
$sumSqOdds = 0;
$sumEven = 0;
$j = 0;


for ($x = 0; $x < $length; $x++)
    {
        if (($numbers[$x] % 2) == 1)
        {
            $odds[$j] = $numbers[$x];
            $sumSqOdds = $sumSqOdds + pow ($numbers[$x], 2);
            $j++;
        }
        else 
        {
            $sumEven = $sumEven + $numbers[$x];
        }
    }

$x = 0;
$y = 0;

printf("The odd numbers between your integers are: ");

for ($x = 0; $x < $j; $x++)
    {
        echo $odds[$x];
        echo '&nbsp;';
        $y++;
        if (($y % 10) == 0)
        {
            echo B;
        }
    }

echo B;
echo B;

printf("The sum of all even numbers between your integers is: ".$sumEven);

echo B;
echo B;

printf("The sum of the square of the odd numbers between your integers is: ".$sumSqOdds);
定义(“B”和“
”); $firstNum=$_POST[“firstNum”]; $secondNum=$_POST[“secondNum”]; 如果($firstNum<$secondNum) { $firstNum=true; } elseif($firstNum>=$secondNum) { $firstNum=“你没听,笨蛋!”。
; } 回显“第一个数字:”.$firstNum.“
。”“第二个数字:”.$secondNum; 回声B; 回声B; $numbers=array(); $numbers=范围($firstNum,$secondNum); $length=计数($number); $lobbits=array(); $sumsqlobbs=0; $sumEven=0; $j=0; 对于($x=0;$x<$length;$x++) { 如果(($numbers[$x]%2)==1) { $赔率[$j]=$数字[$x]; $sumsqlobbits=$sumsqlobbits+pow($number[$x],2); $j++; } 其他的 { $sumEven=$sumEven+$number[$x]; } } $x=0; $y=0; printf(“整数之间的奇数是:”); 对于($x=0;$x<$j;$x++) { echo$赔率[$x]; 回声'; $y++; 如果($y%10)==0) { 回声B; } } 回声B; 回声B; printf(“整数之间所有偶数的总和为:“.$sumEven”); 回声B; 回声B; printf(“整数之间奇数的平方之和为:”.$sumsqloffics);
这是我的while循环,但它似乎是无限的

$numW = array ();
$numW = range ($firstNum, $secondNum);
$lengthW = count ($numW);
$oddsW = array ();
$sumSqOddsW = 0;
$sumEvenW = 0;
$j = 0;
$x = 0;

while ($x < $lengthW)
{
    if (($numW[$x] % 2) == 1)
    {
        $oddsW[$j] = $numW[$x];
        $sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
        $x++;
        $j++;
    }
    else
    {
        $sumEvenW = $sumEvenW + $numW[$x];
    }
}

$x = 0;
$y = 0;

printf ("The odd numbers between your integers are: ");

while ($x < $j)
{
    $x++;
    echo $oddsW[$x];
    echo "nbsp;";
    $y++;
    if (($y % 10) == 0)
    {
        echo B;
    }
}
$numW=array();
$numW=范围($firstNum,$secondNum);
$lengthW=计数($numW);
$oddsW=array();
$sumSqOddsW=0;
$sumEvenW=0;
$j=0;
$x=0;
而($x<$纵向)
{
如果($numW[$x]%2)==1)
{
$oddsW[$j]=$numW[$x];
$sumSqOddsW=$sumSqOddsW+pow($numW[$x],2);
$x++;
$j++;
}
其他的
{
$sumEvenW=$sumEvenW+$numW[$x];
}
}
$x=0;
$y=0;
printf(“整数之间的奇数是:”);
而($x<$j)
{
$x++;
echo$oddsW[$x];
呼应“nbsp;”;
$y++;
如果($y%10)==0)
{
回声B;
}
}
等效循环:

for ($i = 0; $i < 10; $i++) {
  echo $i;
}

$i = 0;
while ($i < 10) {
  echo $i;
  $i++;
}
($i=0;$i<10;$i++)的
{
echo$i;
}
$i=0;
而($i<10){
echo$i;
$i++;
}
等效循环:

for ($i = 0; $i < 10; $i++) {
  echo $i;
}

$i = 0;
while ($i < 10) {
  echo $i;
  $i++;
}
($i=0;$i<10;$i++)的
{
echo$i;
}
$i=0;
而($i<10){
echo$i;
$i++;
}

循环要完成,必须更改两个求值变量之一。因此,$x或$lengthW必须在迭代过程中更改。

您做了一个if声明,在第一种情况下,您定义x增加1,但在另一种情况下,您不会更改任何对$x或$lengthW有影响的变量

也没有任何检查来查看是否已达到else状态,并通过在以后的迭代中更改$x或$lengthway来捕获该状态。

因此,一旦到达else情况,就会有一个无限循环。

if语句使用与上一次迭代相同的$x值来检查$numW的相同位置,因此自上一次迭代以来没有任何变化,您将再次点击else,再点击else,依此类推

while ($x < $lengthW)
{
    if (($numW[$x] % 2) == 1)
    {
        $oddsW[$j] = $numW[$x];
        $sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
        $x++; //$x is increased by one, and as such, the loop will progress.
        // remove this $x++ if you place it outside the if else statement.
        $j++;
    }
    else // reached when ($numW[$x] %2) != 1
    {
        $sumEvenW = $sumEvenW + $numW[$x];
        // No changes to $x or $lengthW as such you'll hit the else again
        // this could be solved by either adding $x++; here.
    }
    // or by adding $x++; here 
    // (if you do add it here, remove it in the if case above, 
    // or you risk increasing it by 2 every iteration
}
while($x<$纵向)
{
如果($numW[$x]%2)==1)
{
$oddsW[$j]=$numW[$x];
$sumSqOddsW=$sumSqOddsW+pow($numW[$x],2);
$x++;//$x增加一,因此循环将继续。
//如果将此$x++放在if-else语句之外,请将其删除。
$j++;
}
else//在($numW[$x]%2)!=1时达到
{
$sumEvenW=$sumEvenW+$numW[$x];
//对$x或$lengthW没有任何更改,因此您将再次点击else
//这可以通过在此处添加$x++;来解决。
}
//或者在此处添加$x++
//(如果您在此处添加,请在上面的if案例中删除它,
//或者你冒着每次迭代增加2的风险
}

循环要完成,必须更改两个计算变量中的一个。因此,$x或$lengthW必须在迭代过程中更改。

您做了一个if声明,在第一种情况下,您定义x增加1,但在另一种情况下,您不会更改任何对$x或$lengthW有影响的变量

也没有任何检查来查看是否已达到else状态,并通过在以后的迭代中更改$x或$lengthway来捕获该状态。

因此,一旦到达else情况,就会有一个无限循环。

if语句使用与上一次迭代相同的$x值来检查$numW的相同位置,因此自上一次迭代以来没有任何变化,您将再次点击else,再点击else,依此类推

while ($x < $lengthW)
{
    if (($numW[$x] % 2) == 1)
    {
        $oddsW[$j] = $numW[$x];
        $sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
        $x++; //$x is increased by one, and as such, the loop will progress.
        // remove this $x++ if you place it outside the if else statement.
        $j++;
    }
    else // reached when ($numW[$x] %2) != 1
    {
        $sumEvenW = $sumEvenW + $numW[$x];
        // No changes to $x or $lengthW as such you'll hit the else again
        // this could be solved by either adding $x++; here.
    }
    // or by adding $x++; here 
    // (if you do add it here, remove it in the if case above, 
    // or you risk increasing it by 2 every iteration
}
while($x<$纵向)
{
如果($numW[$x]%2)==1)
{
$oddsW[$j]=$numW[$x];
$sumSqOddsW=$sumSqOddsW+pow($numW[$x],2);
$x++;//$x增加一,因此循环将继续。
//如果将此$x++放在if-else语句之外,请将其删除。
$j++;
}
else//在($numW[$x]%2)!=1时达到
{
$sumEvenW=$sumEvenW+$numW[$x];
//对$x或$lengthW没有任何更改,因此您将再次点击else
//这可以通过在此处添加$x++;来解决。
}
//或者在此处添加$x++
//(如果您在此处添加,请在上面的if案例中删除它,
//或者你冒着每次迭代增加2的风险
}

对于数组,你最好学习:对于数组,你最好学习:我认为这是一条路线,谢谢,但是,我似乎一直在运行一个无限循环。不确定如何将我的代码添加到评论中,所以我编辑了主要帖子并将其添加到那里。我认为这是一条路线,谢谢,但是,我似乎一直在运行一个Infinite循环。不确定如何添加我的代码t