简体   繁体   English

使用三元运算符的星形模式

[英]star pattern using ternary operator

<?php
for ($row = 1; $row <= $_POST["number"]; $row++)
{
for ($col = 1; $col <= ($row >= ($_POST["number"]/2) ? ($_POST["number"]+1)- $row : $row); $col++)
    {
        echo '* ';
    }

     echo "<br>";
}
print(json_encode(count($row)));

?>

the question is to print the pattern and also the total number of stars in each row.问题是打印图案以及每行中的星星总数。 i tried degugging my self but if i change the condition and only for some inputs the answer is correct.我尝试对自己进行调试,但是如果我更改条件并且仅针对某些输入,则答案是正确的。 最后

You can use the following:您可以使用以下内容:

<?php
$_POST["number"] = 10;
$row_num = array();
for ($row = 1; $row <= $_POST["number"]; $row++)
{
    $count = 0;
    for ($col = 1; $col <= ($row >= ($_POST["number"]/2) ? ($_POST["number"]+1)- $row : $row); $col++)
    {
        $count++;
        echo '* ';
    }
    echo "<br>";
    array_push($row_num, $count);
}
print_r ($row_num);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM