2014-11-03 25 views
0
$testCase=""; 
$strength=""; 
$i="";$j="";$sum=0; 
$submit=$_POST['submit']; 

if(!empty($submit)) 
{ 
    $testCase=$_POST['TestCase']; 
    $strength=$_POST['strength']; 
} 
else 
{ 
    die("Please enter the Number of <strong>test case</strong><br/> and <strong> Strength of audience</strong>"); 
} 
while($testCase>0) 
{ 
    for($i=1;$strength;$i=$i+1) 
    { 
     $user[$i]=$i+1; 
    } 

    //finding the total number of stickers 

    for($j=1;$j<$strength;$j=$j+1) 
    { 
     $sum=$sum+$user[$j]; 
    } 

} 
echo "The total number of <strong>Stickers</strong> are = " . $sum; 
+2

這裏很少有問題。爲什麼當$ 0與0進行比較時,$ testCase被初始化爲一個字符串? – David 2014-11-03 13:43:20

+5

你有一個無限循環,'$ testCase'在循環中永遠不會改變。 – jeroen 2014-11-03 13:44:26

+0

$ testCase沒有被初始化爲任何東西。 – aps240 2014-11-03 13:51:47

回答

0

這條線: 爲($ I = 1; $強度; $ I = $ I + 1) 也許應該是: 爲($ i = 1; $ I < $強度; $ I = $ I + 1)

1

因爲你這樣做:while ($testCase > 0) {不decrase的$testCase永遠。另外,如果你把你的循環放到你的if條件中會更好。

而且您不需要創建$strength變量。試試這個:

$sum = 0; 
if (!empty($_POST["submit"]) && !empty($_POST["testCase"]) && !empty($_POST["strength"])) { 
    $testCase = $_POST["testCase"]; 
    while ($testCase > 0) { 
     for ($i = 1; $i < $_POST["strength"];$i++) { 
      $user[$i] = $i + 1; 
     } 
     for ($j = 1; $j < $_POST["strength"]; $j++) { 
      $sum = $sum + $user[$j]; 
     } 
     //Here you need to decrase testcase 
     $testCase--; 
    } 

} else { 
    die("Please enter the Number of <strong>test case</strong><br/> and <strong> Strength of audience</strong>"); 
} 
echo "The total number of <strong>Stickers</strong> are = " . $sum; 

注意:在PHP中,如果不直接指定鍵,PHP數組總是以0開始。檢查你的for循環。

相關問題