以字母形状打印星形图案但不起作用

时间:2018-02-13 11:44:29

标签: php

我正在尝试改进php中的逻辑,我正在尝试在A形状,B形状和K形状中打印星形图案,但它不起作用,任何人都可以帮助我使用我的逻辑我的代码如下

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>";
}

?>

我的代码没有按预期工作,请任何人帮助

2 个答案:

答案 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>