2017-11-03 16 views
0


私は数時間のような時間を探していますが、解決策は見当たりません。 パスカル三角形をテーブルに表示したいときに問題がありますが、実際には正しく表示されません。 ここでは、コードです: `PHP Pascalの三角形がテーブルに正しく表示されない

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Triangle de Pascal</title> 
     <meta charset="utf-8"> 
    </head> 
    <body> 
     <form action="index.php" method="POST"> 
      <label for="depth">Profondeur : </label> 
      <input type="number" id="depth" name="depth" min="1" max="100"> 
      <input type="submit"> 
     </form> 
     <style> 
      table{ 
       border-collapse: collapse; 
      } 
      td{ 
       text-align: center; 
       padding: 0px 5px; border: 1px solid black; 
       box-sizing: border-box; 
      } 
     </style> 
      <table> 
<?php 
$depth = $_POST['depth']; 
$length = $depth; 
$array[0][0] = 1; 
for ($i = 1; $i < $depth; $i++){ 
    $array[$i][0] = 1; 
    echo "<tr>"; 
     for($j = 1; $j <= $i; $j++){ 
      if($j == $i){ 
       $array[$i][$j] = 1; 
      } 
      else{ 
      $array[$i][$j] = $array[$i-1][$j-1] + $array[$i-1][$j]; 
     } 
     echo "<td colspan=".floor($length * $length/($i)).'>'.$array[$i][$j]."</td>"; 
    } 
    echo "</tr>"; 
} 
?> 
     </table> 
    </body> 
</html> 

あなたは、ループ内の論理エラーを持っている問題

+0

出力として何を期待していますか? – pr1nc3

答えて

1

がどこにあるあなたがその問題を解決するために私を助け場合、これは本当にクールになる、私は本当に、表示されていないが、ここで

<?php 
$depth = $_POST['depth']; 
$length = $depth; 
for ($i = 1; $i < $depth; $i++){ 
    echo "<tr>"; 
     for($j = 1; $j <= $i; $j++){  
      if($j == 1 || $j == $i){ 
       $array[$i][$j] = 1; 
      } 
      else{ 
       $array[$i][$j] = $array[$i-1][$j-1] + $array[$i-1][$j]; 
     } 
     echo "<td colspan=".floor($length * $length/($i)).'>'.$array[$i][$j]."</td>"; 
    } 
    echo "</tr>"; 
} 
?> 

があなたの入力ボックスに値として15を持つ例です:

とき $j==1あなたがすべきコードのPHP部1の出力は、この(私はまた、いくつかのusless初期設定を削除)のようなものでなければなりません

enter image description here

+0

@ YanisBendahmaneはいごめんなさい、私はコード –

+0

を更新します@ YanisBendahmaneの答えは –

+0

を更新しましたありがとう!あなたは素晴らしいです! –

関連する問題