创建棋盘并在每行中生成数字增加 1 PHP?

https://img1.sycdn.imooc.com/65aa3ed10001a18804530437.jpg

我已经使用嵌套 for 循环生成了一个棋盘,但我无法弄清楚在其上显示数字


我的代码


<?php

    $cellNum=2;

      $rowNum=1;

      $color=['#FFFFFF','#000000'];

      $color1='#FFFFFF';

      $color2='#000000';


    for($row=1;$row<=6;$row++){

        echo "<tr>";

        for($col=1;$col<=6;$col++) {

            $total=$row+$col;

            $i=1;

            if($total%2==0) {

                echo "<td height=50px width=50px bgcolor=#FFFFFF align='center'></td>";

            } else {

                echo "<td height=50px width=50px bgcolor=#000000 style='color:white; text-align:center;'></td>";

            }

        }

        echo "</tr>";

    }

?>


SMILET
浏览 56回答 3
3回答

大话西游666

只需将行和列相乘即可。for($row=1;$row<=6;$row++){&nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; for($col=1;$col<=6;$col++) {&nbsp; &nbsp; &nbsp; &nbsp; $total=$row+$col;&nbsp; &nbsp; &nbsp; &nbsp; $i=1;&nbsp; &nbsp; &nbsp; &nbsp; if($total%2==0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td height=50px width=50px bgcolor=#FFFFFF align='center'>".$row*$col."</td>";&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td height=50px width=50px bgcolor=#000000 style='color:white; text-align:center;'>".$row*$col."</td>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; echo "</tr>";}

收到一只叮咚

只需将行和列相乘即可。for($row=1;$row<=6;$row++){&nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; for($col=1;$col<=6;$col++) {&nbsp; &nbsp; &nbsp; &nbsp; $total=$row+$col;&nbsp; &nbsp; &nbsp; &nbsp; $i=1;&nbsp; &nbsp; &nbsp; &nbsp; if($total%2==0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td height=50px width=50px bgcolor=#FFFFFF align='center'>".$row*$col."</td>";&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td height=50px width=50px bgcolor=#000000 style='color:white; text-align:center;'>".$row*$col."</td>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; echo "</tr>";}

繁华开满天机

你离我如此之近,我几乎不敢回答这个问题<?php&nbsp; &nbsp; $cellNum=2;&nbsp; &nbsp; $rowNum=1;&nbsp; &nbsp; $color=['#FFFFFF','#000000'];&nbsp; &nbsp; $color1='#FFFFFF';&nbsp; &nbsp; $color2='#000000';&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; echo '<table border="1">';&nbsp; &nbsp; for($row=1;$row<=6;$row++){&nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; for($col=1;$col<=6;$col++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total=$row*$col;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(($row+$col) % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td height=50px width=50px bgcolor=#FFFFFF align='center'>$total</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td height=50px width=50px bgcolor=#000000 style='color:white; text-align:center;'>$total</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; }&nbsp; &nbsp; echo '</table>';?>给予
打开App,查看更多内容
随时随地看视频慕课网APP