2017-02-16 85 views
0

這是我的代碼。它目前的工作原理是,但我正在試驗循環,並希望看到它可以完成一個while循環以及如何完成。使用這段代碼,我可以輸入2個輸入數字並顯示它們,然後指出所有可能性,添加所有平均數,並添加所有可能性的平方。我是PHP的新手,我創建了一個For循環,想知道是否有人知道如何將它重新寫入while循環?

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); 

這裏是我的,而循環,但它似乎是無限的......

$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; 
    } 
} 
+0

對於數組你最好的學習:http://php.net/manual/en/control-structures.foreach.php – AbraCadaver

回答

0

爲一個循環,不斷完成它有可能改變的一個兩個評估變量。所以$ x或$ lengthW在迭代期間都不得不改變。


您提出了一個if語句,在第一種情況下,您定義X增加1,但在其他情況下,您不會更改任何影響$ x或$ lengthW的變量,也不是還有任何檢查是否已經到達else狀態,並通過在稍後的迭代中更改$ x或$ lengthW來捕獲該狀態。

因此,只要到達else情況,就會出現無限循環。

if語句使用與最後一次迭代相同的$ x值來檢查$ numW的相同位置,因爲自上次迭代以來沒有任何變化,您將再次敲擊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 
} 
+0

謝謝你,清除它。 – acdkmd12

2

等效循環:

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

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

我想這就是走的路線,謝謝,但是,我似乎保持陷入無限循環。不確定如何將我的代碼添加到評論中,所以我編輯了主帖並將其添加到了那裏。 – acdkmd12

相關問題