k.php
<?php
$j = 5;
$i = 0;
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == $column + 1) and $column != 0))
echo "*";
else if ($row == $i and $column == $j)
{
echo "*";
$i=$i+1;
$j=$j-1;
}
else
echo " ";
}
echo "<br>";
}
?>
B.php
<?php
for ($row=0; $row<7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == 0 or $row == 3 or $row == 6) and ($column < 5 and $column > 2)) or ($column == 5 and ($row != 0 and $row != 3 and $row != 6)))
echo "*";
else
echo " ";
}
echo "<br>";
}
?>
A.php
<?php
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ((($column == 1 or $column == 5) and $row != 0) or (($row == 0 or $row == 3) and ($column > 1 and $column < 5)))
echo "*";
else
echo " ";
}
echo "<br>";
}
?>
我的代码没有按预期工作,请任何人帮助
答案 0 :(得分:0)
您可以尝试以下代码:
<pre><?php echo getK();?></pre>
<pre><?php echo getA();?></pre>
<pre><?php echo getB();?></pre>
<?php
function getK()
{
$j = 5;
$i = 0;
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == $column + 1) and $column != 0))
echo "*";
else if ($row == $i and $column == $j)
{
echo "*";
$i=$i+1;
$j=$j-1;
}
else
echo " ";
}
echo "\n";
}
}
function getA()
{
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ((($column == 1 or $column == 5) and $row != 0) or (($row == 0 or $row == 3) and ($column > 1 and $column < 5)))
echo "*";
else
echo " ";
}
echo "\n";
}
}
function getB()
{
for ($row=0; $row<7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == 0 or $row == 3 or $row == 6) and ($column < 5 and $column > 2)) or ($column == 5 and ($row != 0 and $row != 3 and $row != 6)))
echo "*";
else
echo " ";
}
echo "<br>";
}
}
答案 1 :(得分:0)
还有一个答案:
说明:
n = "new line (\n)"
* = letter (you can use, bettor for you)
space = space)))
for example: "a" => " *** n
* *n
* *n
*****n
* *n
* *n
* *",
代码:
<?php
$letters = [
"a" => " *** n* *n* *n*****n* *n* *n* *",
"b" => "**** n* *n* *n****n* *n* *n**** ",
"k" => "* *n* *n* *n**n* *n* *n* *",
];
function printLetter($pattern)
{
$len = strlen($pattern);
for ($i = 0; $i < $len; $i++) {
if ($pattern[$i] == 'n') {
echo "\n";
continue;
}
echo $pattern[$i];
}
}
?>
<pre><?php printLetter($letters['a']); ?></pre>
<pre><?php printLetter($letters['b']); ?></pre>
<pre><?php printLetter($letters['k']); ?></pre>